[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-879) Enable joining the same association twice with Criteria
Ashkan Aryan (JIRA)
noreply at atlassian.com
Tue Feb 1 10:34:09 EST 2011
[ http://opensource.atlassian.com/projects/hibernate/browse/HHH-879?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=39793#action_39793 ]
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.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira
More information about the hibernate-issues
mailing list