-->
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: question about disjunction between 2 associated objects
PostPosted: Tue Feb 07, 2006 4:46 pm 
Newbie

Joined: Mon Feb 06, 2006 6:18 pm
Posts: 3
Hi,
I am using Hiberate 2.1.8 and criteria api to write a search screen.
I think Criteria API is good in this scenario by avoiding a lot of dynamic hql concatenation. But now I tried to do something like:

Object A contains Object B (you can get B by A.B)
A has an attribute name
B has an attribute title
A and B are associated with A.id = B.id

One of the search criteria is

A.name = 'input search string'
or A.B.title = 'input search string'

I cannot find a way to use criteria query(conjunction or disjunction) since A and B are not in the same level. I saw several postings on similar subject before but haven't see a clear answer for Hibernate 2. My question is this at all possible in Hibernate 2? If so, how?

Thank you in advance!

-Jimmy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 5:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Here's a verbose solution. You can do it with fewer Criteria objects, but this self-documents:
Code:
Criteria crit1 = session.createCriteria(A.class);
crit.add(Restrictions.eq("name", string);
Criteria crit2 = session.createCriteria(A.class);
crit2.createCriteria("B").add("title", string);
Criteria crit = session.createCriteria(A.class);
crit.add(Restrictions.or(crit1, crit2));


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 6:22 pm 
Newbie

Joined: Mon Feb 06, 2006 6:18 pm
Posts: 3
Thanks a lot Tenwit for the quick reply.
I think Restrictions and Criterion interface are only available in Hibernate 3. I have to use Hibernate 2 for this project.

BTW, I have just figured out a way to solve my problem. I can create an alias for the internal object B(name it myB). And in the outer criteria I can reference B.title via myB.title and this can be in the same Disjunction as A.name which is what I need.


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.