-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Criteria with junctions throws "duplicate association p
PostPosted: Thu Dec 06, 2007 10:32 am 
Newbie

Joined: Wed Sep 06, 2006 7:32 am
Posts: 6
I've got a domain model which looks a bit like this:

A
==
B getB()
C getC()

B
==
String getName()

C
==
D getD()

D
==
String getName()
String getTown()

and I'm trying to create the following where clause using the Criteria API:

where c.d.name = "wibble" or (c.d.town="blah" and b.name="wobble")

The code I'm using is this:

Criteria rootCriteria = session.createCriteria(A.class);
Criteria or = rootCriteria.add(Restrictions.disjunction());
or.createCriteria("c")
.createCriteria("d")
.add(Restrictions.eq("name", "wibble"));
Criteria and = or.add(Restrictions.conjunction());
and.createCriteria("c")
.createCriteria("d")
.add(Restrictions.eq("town", "blah");
and.createCriteria("b")
.add(Restrictions.eq("name", "wobble"));

List result = rootCriteria.list();

However, running this gives me the following exception:
org.hibernate.QueryException: duplicate association path: c
at org.hibernate.loader.criteria.CriteriaQueryTranslator.createAssociationPathCriteriaMap(CriteriaQueryTranslator.java:141)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.<init>(CriteriaQueryTranslator.java:80)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:58)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1550)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)

Is there any way to restructure my criteria to give me the where clause I'm looking for?


Hibernate version: 3.2.5


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 8:42 am 
Newbie

Joined: Wed Sep 06, 2006 7:32 am
Posts: 6
Right, I've solved this - in case anyone else comes across this post, this is the code snippet - basically you need to use aliases:

Criteria rootCriteria = session.createCriteria(A.class);
rootCriteria.createAlias("b", "b");
rootCriteria.createAlias("c", "c");
rootCriteria.createAlias("c.d", "d");

rootCriteria.add(Restrictions.disjunction()
.add(Restrictions.eq("d.name", "wibble"))
.add(Restrictions.conjunction()
.add(Restrictions.eq("d.town", "blah"))
.add(Restrictions.eq("b.name", "wobble")));

List result = rootCriteria.list();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.