[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-879?page=co...
]
Ashkan Aryan commented on HHH-879:
----------------------------------
With regards to Simon's suggestion above, it does work, however there are a couple of
issues you need to take into account while implementing it. Here's an example of how I
did it.
relationship : ClassA- 1 : 0...* -ClassB
Assuming that you already have a criteria for ClassA, called classACriteria, you will need
to add something like this to it:
{code:title=sample|borderStyle=solid}
Conjunction conjunction = Restrictions.conjunction();
DetachedCriteria detachedCriteria1 =
DetachedCriteria.forClass(ClassA.class).setProjection(Projections.id());
detachedCriteria1.createAlias("classB",
"classBAlias1"); //classB is the name of property representing a bag of ClassA
instances in ClassB
detachedCriteria1.add(Restrictions.eq("classBAlias1.field1",
value1)); //Replace field one with the appropriate property name
detachedCriteria1.add(Restrictions.eq("classBAlias1.field2",
value2)); //
DetachedCriteria detachedCriteria2 =
DetachedCriteria.forClass(ClassA.class).setProjection(Projections.id());
detachedCriteria2.createAlias("classB",
"classBAlias2");
detachedCriteria2.add(Restrictions.eq("classBAlias2.field3",
value3));
detachedCriteria2.add(Restrictions.eq("classBAlias2.field4",
value4));
conjunction.add(Property.forName("id").in(detachedCriteria1));
conjunction.add(Property.forName("id").in(detachedCriteria2));
classACriteria.add(conjunction);
{code}
Please note, you'll have to specifically set the projection on each criteria (in this
case we're assuming that ClassB has a unique identifier) otherwise you'll get a
nasty NPE.
As mentioned before this is not ideal (a proper join would've been preferable in terms
of performance) but at least it works!
Enable joining the same association twice with Criteria
-------------------------------------------------------
Key: HHH-879
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-879
Project: Hibernate Core
Issue Type: Improvement
Components: core
Reporter: Vladimir Bayanov
Make double joining the same association with Criteria.createCriteria possible. See:
http://forum.hibernate.org/viewtopic.php?t=931249
--
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators....
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira