aspose file tools*
The moose likes Object Relational Mapping and the fly likes AND/OR in Hibernate - Criteria object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "AND/OR in Hibernate - Criteria object" Watch "AND/OR in Hibernate - Criteria object" New topic
Author

AND/OR in Hibernate - Criteria object

Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 40
I have a query like "select
this_.appNum as y0_
from
CoreApplication this_
inner join
LoanAndDeposit loananddep2_
on this_.appNum=loananddep2_.CoreApplication_appNum
inner join
OptionTrade optiontrad1_
on this_.appNum=optiontrad1_.CoreApplication_appNum
where
this_.applicationType in (
?, ?, ?, ?
)
and this_.status in (
?, ?, ?
)
and optiontrad1_.lastModifiedUser=?
and loananddep2_.status=? ".
For the last part i.e. OptionTrade.lastModifiedUser and LoanAndDeposit.status I have to do an OR and not an AND. These are generated by sub-Criteria objects are here's the code :

I have an idea that this is done by Conjunction object but can anyone please tell me how in this case ?? Thanks in advance.
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi Somak, I suggest You use Restrictions instead Expression. You must remember Expression supports nested Expression(s):

Expression.or(Expression1, Expression2);


I hope it helps


"La verdadera sabidur´┐Ża viene del temor de Dios"
Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 40
Hi Halcon, thanks for the reply - I want to create an OR between

and

Can you tell me how can I acheive that ? I want an OR between these two sub-Criteria-s.
Halcon Guatemala
Ranch Hand

Joined: Sep 06, 2006
Posts: 57
Hi again Somak, sorry but I didn't know about your 2 criterias You want to do "or" between. It seems any case of sql "union" clause, You know it isn't supported by Criteria API, You might considerate to use SQLQuery for it...

I hope this helps
Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 40
Thanks a ton Halcon, been searching for the solution for a long time.
Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 40
Just find out that this is possible by creating alias.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi,

Just curious whether Restrictions.disjunction(); would help you in any way?

e.g.



This is the Restriction version of OR (conjunction is obviously the AND equivelent)

Would this work for you?

John
Somak Dalui
Ranch Hand

Joined: Apr 19, 2005
Posts: 40
Yes John this will work, but I have to create an alias before I can refer the properties.Because the properties that I am referring to belongs to the mapped class and not to the parent class.So I am doing

This gives me a fine SQL.
 
Consider Paul's rocket mass heater.
 
subject: AND/OR in Hibernate - Criteria object