-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Determine if Criteria will produce a query with joins
PostPosted: Mon Mar 09, 2009 8:28 pm 
Newbie

Joined: Fri Apr 13, 2007 12:25 pm
Posts: 17
Does anybody know of a reliable way to determine if a Criteria's query plan will contain joins?

I can use the CriteriaImpl.iterateSubcriteria to check for the existence of a sub-criteria, but there's also a scenario in which there are associations with a JOIN-based FetchMode, which can be specified at runtime or in the SessionFactory config.

I would like to be able to inspect a regular Criteria (or DetachedCriteria) object to determine whether it's going to be joined, as this has a bearing on how the resulting query's limit clause should be applied for paging.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 09, 2009 9:33 pm 
Newbie

Joined: Fri Apr 13, 2007 12:25 pm
Posts: 17
For anyone who might be interested, I was able to solve the problem like this. Anyone see any glaring problems? It seems to run pretty quickly on my domain model, but I could imagine this causing problems if you had a bunch of classes or classes having tons of persistent properties.

Code:
private boolean hasSubCriteria(Criteria c, Session s){
       CriteriaImpl impl = (CriteriaImpl) c;
       if(impl.iterateSubcriteria().hasNext()) return true;
       else return false;
}
   
private boolean hasFetchJoins(Criteria c, Session s){
       CriteriaImpl impl = (CriteriaImpl) c;
       Map<String,ClassMetadata> metaMap = (Map<String,ClassMetadata>) s.getSessionFactory().getAllClassMetadata();
       for(String persistentClassName : metaMap.keySet()){
          String[] props = metaMap.get(persistentClassName).getPropertyNames();
          for(int i=0; i<props.length; i++){
              if(FetchMode.JOIN.equals(impl.getFetchMode(props[i]))) return true;
          }
       }
       return false;
}


Top
 Profile  
 
 Post subject: Re: Determine if Criteria will produce a query with joins
PostPosted: Wed Aug 12, 2009 4:06 am 
Beginner
Beginner

Joined: Tue Nov 06, 2007 5:13 am
Posts: 28
the problem I see here is, that someone can set an explicit fetch on the criteria
Code:
criteria.setFetchMode()


your logic would not detect this - and it seems very difficult to get this information from the criteria that you have, because there is no way to directly access the fetchModes-map from CriteriaImpl

so what you could do about this:
* use reflection to access the private member
* iterate over all possible fetch-joins (using the info from the metadata) and find out if any of these exist by calling CriteriaImpl.getFetchMode()


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.