I have been having a problem using nested criteria to place restrictions on properties of child objects. It turns out that the problem was solved when I moved from using DetachedCriteria objects to executable Criteria objects. Given the following relationship:
Code:
Class Bid {
Item item;
}
Class Item {
Property p1;
}
I wanted to query for all Bids with Items whose property p1 = X. I set up a simple mapping with a many-to-one reference from Bid to Item, and tried to execute the following code, which failed with a HibernateQueryException: duplicate association path:
Code:
DetachedCriteria criteria = DetachedCriteria.forClass(Bid.class);
criteria.createCriteria("item","i").add(Restrictions.eq("p1",X));
Collection col = getHibernateTemplate.findByCriteria(criteria)
After struggling for two days, I tried the same thing with executable criteria, and succeeded:
Code:
Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(Bid.class);
criteria.createCriteria("item","i").add(Restrictions.eq("p1",X));
Collection col = criteria.list();
I know that findByCriteria is a Spring function, but all it does is call DetachedCriteria.getExecutableCriteria(Session session), so I don't think it's a Spring problem.
Does anyone know why the DetachedCriteria query fails?
Thanks,
Jacob