-->
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.  [ 7 posts ] 
Author Message
 Post subject: Newbie: How to use Criteria to Query this?
PostPosted: Thu Nov 13, 2008 12:12 am 
Newbie

Joined: Sun Aug 05, 2007 8:01 pm
Posts: 4
1. How can I use "Criteria.class" to do such things?

Code:
String hql = "FROM Cat cat WHERE cat IN (?) AND ...";


2. There are a many-to-many relation between Cats.
Cat.java
Code:
private Set<Cat> friends    = new HashSet<Cat>(0);


How to use "Criteria.class" to find cats which having relation of a List<Cat>.

Thank You.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 12:39 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
For this kind of query you need to access cat table twice and as far as I know that not possible through criteria queries, i.e you can join twice to a table through criteria query.

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 12:58 am 
Newbie

Joined: Sun Aug 05, 2007 8:01 pm
Posts: 4
sethrohit1977 wrote:
For this kind of query you need to access cat table twice and as far as I know that not possible through criteria queries, i.e you can join twice to a table through criteria query.

How about

Cat.java
Code:
private Set<Dog> friends    = new HashSet<Dog>(0);


Between Cat and Dog table, there have a relation table. Thank You.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 1:06 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
List data = sess.createCriteria(Cat.class).createCriteria("friends").add(Restrictions.isNotNull("id")).list();

assuming "id" is a valid property in Dog.

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 2:10 am 
Newbie

Joined: Sun Aug 05, 2007 8:01 pm
Posts: 4
sethrohit1977 wrote:
List data = sess.createCriteria(Cat.class).createCriteria("friends").add(Restrictions.isNotNull("id")).list();

assuming "id" is a valid property in Dog.


If I have a List<Dog> dogs, is there any way to query:

sess.createCriteria(Cat.class).createCriteria("friends").add(Restrictions.in(........., dogs)).list();?

Thank You


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 2:15 am 
Regular
Regular

Joined: Mon Jan 22, 2007 10:32 am
Posts: 101
May be I am not able to get your question. If you can explain what you need to do along with relationship between entities, I might be able to help.

_________________
Please rate this post if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 13, 2008 2:26 am 
Newbie

Joined: Sun Aug 05, 2007 8:01 pm
Posts: 4
sethrohit1977 wrote:
May be I am not able to get your question. If you can explain what you need to do along with relationship between entities, I might be able to help.


I already have a list of dogs, and I would like to find related cats.

DB:
[cat] --+-- [cat_and_dog_relation] --+-- [dog]

in Cat.java
Code:
int catid;
[attributes....]
private Set<Dog>    dogs    = new HashSet<Dog>(0);


in Dog.java
Code:
int dogid;
[attributes....]
private Set<Cat>    cats    = new HashSet<Cat>(0);


At first, I query:

Code:
List<Dog> dogs = getDogsGroup(1);
Criteria crit = session.createCriteria(Cat.class);
Criteria dogCrit = crit.createCriteria("dogs");
??????


And how to continue? Thank You Very Much.


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