-->
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 QUERY FOR CHILD Collection
PostPosted: Thu Nov 15, 2007 5:58 pm 
Newbie

Joined: Mon Nov 12, 2007 9:30 pm
Posts: 2
Location: Sunnyvale
I am new to hibernate. I have one to many relationship

select a.*, b.* from A a, B b
where a.id = b.id and
a.name = "rus%" and
b.list in (22, 3,3,4);


I am using Query by example partially in follwoing criteria

Created example for A, and B, where instance ==A and instance.getBs == Collection B object

Criteria c = getSession().createCriteria("A");
c.add(Example.create(instance));


if(instance.getBs() !=null){
c.add(Example.create(instance.getBs()));

??????????????????????? Can i apply example restriction or
??????????????????????? how exactly to compare list of children, using criteria query

}


Appreciate your response..

Regards,
Rushahb


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 15, 2007 7:19 pm 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
Using example with join is quite a bad idea according to my experiences.

btw try a c.createCriteria("B") to add restriction of B table.
Using a colection to create an Example is also a bad idea in my opinion, Example.create(entity) takes an entity == a single object.

Try
Criteria c = getSession().createCriteria("A");
c.add(Restrictions.like("name","rus%");
subcriteriaOnB = c.createCriteria("B");
subcriteriaOnB.add(Restriction.in(.....

You can try Example to replace Restrictions, but I'm not sure it will do the job.

edit: http://www.hibernate.org/hib_docs/v3/re ... a-examples
It seems that I was wrong, that should work with find by example.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 16, 2007 10:39 pm 
Newbie

Joined: Mon Nov 12, 2007 9:30 pm
Posts: 2
Location: Sunnyvale
Thanks for your response.
It really dont make sense to use the example. I totally agree.

Criteria c = getSession().createCriteria("A");
c.add(Restrictions.like("name","rus%");
subcriteriaOnB = c.createCriteria("B");
subcriteriaOnB.add(Restriction.in(.....

But when i use the above criteria query, i have one more problem,

Assume:
A --> B is 1 --> Many

A ==> 2 values
i.e (pk) ==> 1, 2

B ==> 8 values
i.e (pk) ==> 1,2,3,4,5,6,7,8
i.e (fk) ==> 1,1,1,1, 2,2,2,2

When i put some criteria on B, for example :

Criteria c = getSession().createCriteria("A");
c.add(Restrictions.like("name","rus%");
subcriteriaOnB = c.createCriteria("B");
subcriteriaOnB.add(Restriction.in("B.pk", 1,2,5,6)

c.list()

I get total 4 results (1,1), (1,2), (2,5),(2,6) ::

My problem :: can i get only 2 values i.e of A in the result set
And later an iterate through B's values

Do let me know if its possible. (if so, how???)


(Bascially, i want to get only A's values in resultset (but at the same time applying Filters on B )


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.