-->
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: Writing QBE that handles associations - how do I detect?
PostPosted: Thu Jul 20, 2006 10:43 am 
Newbie

Joined: Fri Jul 01, 2005 4:36 pm
Posts: 11
Location: US
Greetings fellow Hibernaters.

I am using HBN 3.2 CR1. I recently discovered that Query by Example does not handle associations (ie collections). I am referring to this http://www.hibernate.org/hib_docs/refer ... a-examples
sort of QBE usage.

So I'm trying to roll my own QBE that handles associations, and does it generically/dynamically.

Given an example instance that indicates the QBE params, how can I *dynamically* tell that a particular property on that instance is an association? Once I know this, I will .add() the appropriate Restrictions.eq("foo", foo) to my criteria. Nulls are just ignored.

For example: (w/ thanks to Nebob in http://forum.hibernate.org/viewtopic.ph ... 766d840a9a)

MyFoo foo = new MyFoo();
MyBar bar = new MyBar();
foo.setID(1);
bar.setFoo(foo);
Example ex = Example.create(bar);
List bars = dbSession.createCriteria(bar.getClass())
.add(ex)
.add(Restrictions.eq("foo", foo)
//other association restrictions here
.list();

How can I dynamically tell that the foo property of bar is an association?

I obviously have to utilize some sort of HBN mapping metadata methods in the API, but what/how?

Any advice appreciated.

aj


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 3:02 pm 
Newbie

Joined: Fri Jul 01, 2005 4:36 pm
Posts: 11
Location: US
OK - I answered my own question here. In order to make QBE recognize associations, you can use: Property.Type.isAssociationType()

For example:
Code:
public List<T> findByExample(T exampleInstance) {
        return findByCriteria(exampleInstance,Example.create(exampleInstance).enableLike(MatchMode.EXACT));       
    }

protected List<T> findByCriteria(T exampleInstance,Criterion... criterion) {
        Criteria crit = getSession().createCriteria(getPersistentClass());
   
        for (Criterion c : criterion) {         
            crit.add(c);           
        }
       
        PersistentClass pc= HibernateUtil.getConfiguration().getClassMapping(getPersistentClass().getName());   
     
        Iterator itProps = pc.getPropertyIterator();
        while ( itProps.hasNext()) {
            Property prop = (Property)itProps.next();       
            if(prop.getType().isAssociationType()) {
                Getter g = prop.getGetter(getPersistentClass()) ;               
                try {                                      crit.add(Restrictions.eq(prop.getName(),g.getMethod().invoke(exampleInstance))) ;
                } catch (IllegalAccessException iae) {
                    System.out.println("Exception: "+iae.getMessage()) ;                           
                } catch (InvocationTargetException ite) {
                    System.out.println("Exception: "+ite.getMessage()) ;
                }           
            }       
        }
        return crit.list();
   }


If you like this, please give me credits...

aj


Top
 Profile  
 
 Post subject: Where is getPersistentClass()
PostPosted: Tue Jan 09, 2007 1:15 am 
Newbie

Joined: Mon Oct 18, 2004 1:42 pm
Posts: 10
Location: Ottawa, Canada
Where is the getPersistentClass() method defined?


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.