You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by "Alexander Lamb (dev)" <al...@mac.com> on 2007/11/14 14:29:28 UTC

Prefetching to-many relationships 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