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