Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AND/OR in Hibernate - Criteria object

 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Halcon, been searching for the solution for a long time.
 
Somak Dalui
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just find out that this is possible by creating alias.
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic