-->
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: Criteria and one-to-many and many-to-many relations
PostPosted: Fri Apr 11, 2008 1:58 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Code between sessionFactory.openSession() and session.close():
<Contact> contact = session.createCriteria(T1.class).add(Restrictions.eq("name", "Tom")).list();

Name and version of the database you are using:
mySQL 5

The generated SQL (show_sql=true):
select T1.property, T2.property, T3.id
from T1
left join T2 on T1.id = T2.id
left join T3 on T1.idd = T3.id

Question:
I do get only Criteria Examples which does have Criterions on just one table. I do have aan Entity T1 which has a Attribute (list<T2> t2list) with a List of Entities from Table T2 (in a one-to-many relation). How can I search for properties of this T2 Objects in the list with criteria?

Is there even a possibility to search for criterias on many-to-many-relations?
Example:
Entity T1 with a Relation to T1_T2 (only IDs) Objects
Entity T2 with a Relation to T1_T2 (only IDs) Objects

I want to search within my Criteria(T1.class) for a property of Entity T2 through Entity T1_T2.

Thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 3:57 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
How to solve it:

Code:
List<Contact> contact = session.createCriteria(T1.class)
.createCriteria("T2").add(Restrictions.eq("property", "value")).list()


My next problem: I cant search through 2 different many-to-many Relations:

Code:
List<Contact> contact = session.createCriteria(T1.class)
.createCriteria("T2").add(Restrictions.eq("property", "value"))
.createCriteria("T3").add(Restrictions.eq("property3", "value"))
.list()


The Criteria class tries to get the property3 Attribute from the T2 Entity. This attribute is in the T1 Entity... anybody got an idea?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 4:27 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
I did solve it =) :

Code:
List<Contact> contact =
         session.createCriteria(T1.class)
            .createAlias("T2", "t22")
            .createAlias("T3", "t33")
            .add(Restrictions.eq("t22.property", "value"))
            .add(Restrictions.eq("t33.property", "value"))
            .list();


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.