Prefetching to-many relationships with Cayenne 3

From: Alexander Lamb (dev) ("Alexander)
Date: Wed Nov 14 2007 - 08:29:28 EST

  • Next message: Andrus Adamchik: "Re: Batch faulting with Cayenne 3"

    Hello list,

    Again, about my performance issues.

    To avoid thow round trips to fetch to-one relationships, we found a
    temporary solution but it doens't seem quite perfect.

    We override the get-to-many relationship in the following way:

    @SuppressWarnings("unchecked")
    public List<Role> getRoles () {
         Expression exp = ExpressionFactory.matchExp("person", this);

         SelectQuery query = new SelectQuery(Role.class, exp);
         query.addPrefetch("profile");
         query.addPrefetch("person");

         if(_refreshRoles) {
           query.setCachePolicy(QueryMetadata.LOCAL_CACHE_REFRESH);
           _refreshRoles = false;
         } else {
           query.setCachePolicy(QueryMetadata.LOCAL_CACHE);
         }

         return getObjectContext().performQuery(query);
    }

    This works, but it has three problems:

    1)
    It forces us to write a lot of rather generic code

    2)
    I need to keep a local variable to manage the refresh (_refreshRoles
    in this case)

    3)
    It is not setting the actual to-many relationship of the source
    object, meaning that if I do an addToRoles or removeFromRoles, not
    only will it fault the actual toManyList but this list will not be
    refreshed unless at the same time I set to true my _refreshRoles
    variables.

    My question is therefore:

    1)
    Is it planned in the near future to have prefetching defined in the
    model?

    2)
    If not, could I simply test if the relationship is of the Fault class
    and if so, do the fetch as I do it and then create a
    org.apache.access.ToManyList object and set its content, then call
    writeProperty("roles", myToManyList)

    Then, to refresh I would simply refault the relationship.

    Thanks,

    Alex



    This archive was generated by hypermail 2.0.0 : Wed Nov 14 2007 - 08:30:35 EST