Re: 1-to-many relationships do not update on new query

From: Bryan Lewis (brya..aine.rr.com)
Date: Tue Sep 07 2004 - 11:25:08 EDT

  • Next message: Timothy Fisher: "Select MAX query"

    Okay, it looks like my problem is a little different; I'll try to
    describe it better. After seeing Peter's follow-up I retried
    addPrefetch(), and I can see that it does cause a more complete fetch.

    Without the addPrefetch, this was the SQL generated each time I entered
    my Employee List page (omitting the column names for brevity):

       SELECT t0.* FROM EMPLOYEE t0 WHERE t0.IsActive = ? [bind: 'true']

    With the addPrefetch I see this extra SQL each time:

       SELECT DISTINCT t0.* FROM ROLE t0, EMPLOYEE_ROLE t1, EMPLOYEE t2
       WHERE t0.Role_ID = t1.Role_ID AND t1.Employee_ID = t2.Employee_ID
       AND (t2.IsActive = ?) [bind: 'true']

    That's fine... it's making sure it's got the latest list of roles for
    _all_ the active employees. The part that's still missing is the
    redetermination of the roles for _each_ employee. The page calls
    getRoles() for each employee. The first time the page is rendered,
    I see an extra SQL statement for each employee:

       SELECT DISTINCT t0.* FROM DWROLE t0, EMPLOYEE_ROLE t1
       WHERE t0.Role_ID = t1.Role_ID AND (t1.Employee_ID = ?) [bind: 13]

    On subsequent renderings that doesn't happen, with the result that
    changes to an employee's roles in one session don't appear in another
    session.

    Peter Backx wrote:
    >
    > Sorry for the late reply, but I've been busy. Adding the "addPrefetch"
    > method to the query did solve the problem in my case.
    >
    > The DataContexts are in different JVMs so that might be the reason. I
    > don't think the problem exists within the same JVM. I'm not entirely
    > sure because I've recently changed the program to use one DataContext
    > bound to the thread. I don't remember any problems within one JVM before
    > that though. And the relationship is 2-way.
    >
    > Thanks a lot for the help,
    > Peter
    >
    >
    > Bryan Lewis wrote:
    >
    >> I've come across the same behavior, I think. I have three Oracle
    >> tables, Employee, Role, and a join table EmployeeRole. Employee has a
    >> 1-to-many relationship "roles"; it's a flattened relationship
    >> Employee->Employee_Role and Employee_Role->Role.
    >>
    >> The first query for a list of employees works fine. I can see from
    >> the SQL logging that the employees are fetched and then each
    >> employee's roles are fetched.
    >>
    >> When the query is repeated, only the employees are refetched. If some
    >> other user session (in another browser -- same localhost-server JVM,
    >> different DataContext) has changed an employee's roles, the change is
    >> not seen in the first session.
    >>
    >> I tried adding query.addPrefetch("employeeRoles.role") but that didn't
    >> help. I tried invalidateObjects() and query.setRefreshingObjects(true).
    >>
    >> Other details:
    >> * There is a corresponding reverse relationship Role.employees.
    >> * A simpler 1-to-1 relationship didn't have any trouble.
    >> * I'm using version 1.1M6.
    >>
    >> HTH,
    >> Bryan
    >>
    >>
    >> Andrus Adamchik wrote:
    >>
    >>> Hi Peter,
    >>>
    >>> Does this happen in different DataContexts in the same JVM? Cayenne
    >>> should handle this case, but there is still some room for mistake
    >>> when automatic refresh can't be done properly. If you have any more
    >>> details on that, I'd appreciate it (e.g. is there
    >>> Object2.getObject1() relationship?).
    >>>
    >>> To explicitly deal with this problem you can use prefetching in the
    >>> query, e.g.:
    >>>
    >>> query.addPrefetch("object2s");
    >>>
    >>> Also, as far as cache policies are concerned, they are not related to
    >>> this case. Cache policies deal with caching *lists* of objects, not
    >>> the objects themselves.
    >>>
    >>> Andrus
    >>>
    >>>
    >>> On Sep 2, 2004, at 9:37 AM, Peter Backx wrote:
    >>>
    >>>> I'm currently using Cayenne (1.1B2) with MySQL for a 3 table
    >>>> database with a few relationships. The database itself is changed by
    >>>> different programs concurrently and when I try to update the view in
    >>>> one program it works ok for basic objects, however if objects were
    >>>> added to a 1-to-many relationship these don't seem to be updated.
    >>>>
    >>>> Maybe to make it more clear: I have Object1 that has a 1-to-many
    >>>> relationship with Object2. If I get Object1 through a query the
    >>>> first time everything works ok and Object1.getObject2s() works fine.
    >>>> However if new Object2's are added to that relationship (from
    >>>> another thread/program/DataContext) and I perform a new query to get
    >>>> Object1 I still get the old list of Object2's. So I don't see the
    >>>> newly added relationships.
    >
    >
    >
    >



    This archive was generated by hypermail 2.0.0 : Tue Sep 07 2004 - 11:25:27 EDT