Reputation: 8259
I have a class AttributeGroup
that has a many-to-one relationship with a class Attribute
. Given either an AttributeGroup
name or an Attribute
name or both, I'd like to return an appropriate join of the data. Here is my initialization code:
Criteria criteria = HibernateUtil.getCurrentSession()
.createCriteria(AttributeGroup.class)
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
That's all fine. If I return that List
by calling criteria.list()
, I get every AttributeGroup
in my database. What's odd is if I add this line next:
Criteria attributesCriteria = criteria.createCriteria("attributes");
In that case, an empty List
is returned. What's happening? My understanding is that attributesCriteria
should have no effect until I add a Restriction
. What I would like to do next is this:
if (attribute != null) {
attributesCriteria.add(Restrictions.eq("name", attribute));
}
Thanks.
Upvotes: 0
Views: 56
Reputation: 581
Try this and Add restriction to below criteria.
Criteria criteria = HibernateUtil.getCurrentSession()
.createCriteria(AttributeGroup.class)
.createCriteria("attributes").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
createCriteria
and almost all Criteria
methods modify the instance they're invoked on, and return the instance itself for method chanining. So you can chain calls like above.
Upvotes: 1