-->
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.  [ 1 post ] 
Author Message
 Post subject: How to create example with recursively added associations?
PostPosted: Tue Oct 21, 2008 8:45 am 
Newbie

Joined: Fri Dec 29, 2006 5:56 am
Posts: 4
Location: Copenhagen, Denmark, Europe
The documentation seems to imply that associations are not automatically included in the query by example.

http://www.hibernate.org/hib_docs/v3/reference/en/html/querycriteria-examples.html

But wouldn't it make sense to have a static factory method like Example.create(...) that included associations?

Something like

Code:
   public Criteria addExampleAndAssociatedExamples(Criteria c, Object entity){
      c.add(Example.create(entity));
      
      final SessionFactoryImplementor impltor = (SessionFactoryImplementor)getSession().getSessionFactory();
      final EntityPersister ep = impltor.getEntityPersister(entity.getClass().getName());
      
      final String[] pNames = ep.getPropertyNames();
      final Type[] pTypes = ep.getPropertyTypes();

      for(int i = 0; i < pTypes.length; i++) {
         final Type type = pTypes[i];
         final String name = pNames[i];
         if(type.isAssociationType() ){
            final Object val = ep.getPropertyValue(entity, name, ep.guessEntityMode(entity) );
            if( val != null){
               addExampleAndAssociatedExamples(c.createCriteria(name), val);      
            }         
         }
      }
      return c;
   }


Is there a better way?

Best,
Anders


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

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.