-->
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.  [ 3 posts ] 
Author Message
 Post subject: "createCriteria" and traveling back the associati
PostPosted: Mon Oct 10, 2005 3:08 pm 
Newbie

Joined: Mon Oct 10, 2005 2:48 pm
Posts: 1
Hello Hibernate folks,

This is my first posting here, and i hope i'm not being totally noobish with the following question.

I've got the following code:

Code:
Criteria crit = this.getSession().createCriteria(MainClass.class);


The MainClass is linked to other classes (let's call them "MemberClass" -> member name "memberClasses") which themselves contain other classes (let's call them "subClasses" <- member name)


I want to get all MainClass objects which own one or more memberClasses which own one or more subClasses with the id "someRandomId":

Code:
crit = crit.createCriteria("memberClasses").createCriteria("subClasses").add(Restrictions.eq("id", someRandomId));



This one works really well, but it doesn't allow me to add

Code:
crit = crit.createCriteria("memberClasses").createCriteria("subClasses2").add(Restrictions.eq("id", someOtherRandomId));


to the above code because i've already changed the associationPath to "memberClasses->subClasses". Is there a way to get back to "memberClasses" and then go to "memberClasses->subClasses2" or any other way to reach my desired goal? ;)

thanks,
Marc


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 4:30 pm 
Regular
Regular

Joined: Tue Mar 01, 2005 2:35 pm
Posts: 60
How about this:

Code:
Criteria base = getSession().createCriteria(MainClass.class)
   .createCriteria("memberClasses");

Criteria one = base.createCriteria("subClasses")
   .add(Restictions.idEq(12);

Criteria two = base.createCriteria("subClasses2")
   .add(Restrictions.idEq(13);


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 4:35 pm 
Newbie

Joined: Mon Jul 18, 2005 7:45 pm
Posts: 15
Location: Argentina
Marc,

You have to create an Alias.
Do something like

Code:
crit=crit.createAlias("subClasses","a");
crit=crit.createAlias("subClasses2","b");
crit=crit.add(Restrictions.eq("a.id", someRandomId);
crit=crit.add(Restrictions.eq("b.id", someOtherRandomId);


I hope this helps
Daniel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.