-->
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: OR with multiple Criteria trouble
PostPosted: Thu Mar 06, 2008 11:25 am 
Newbie

Joined: Thu Mar 06, 2008 10:38 am
Posts: 1
Hi, I have the following class
Code:
class File {
  List<Document> documents;
  List<Invoice> invoices;
}


properly mapped, actually Its far more complex but I want to keep it simple to explain my problem.
Document and Invoice classes has a "notes" property. I want to search all the files where the notes of documents OR the notes of invoices are equals to some value. The whole is a more complex query and I cant use HQL.
Code:
Criteria criteria = session.createCriteria("File");
Criteria criteriaDocument = criteria.createCriteria("Document");
Criteria criteriaInvoice = criteria.createCriteria("Invoice");

   criteriaDocument.add(Restriction.ilike("notes","somevalue"));
   criteriaInvoice.add(Restriction.ilike("notes","somevalue"));


I cant use Restriction.or() because its criteria aware. The SQL generated is "document.notes = 'something' AND invoice.notes='something'" but I cant find a way to do some OR between those criterias.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 07, 2008 4:36 am 
Beginner
Beginner

Joined: Thu Nov 15, 2007 11:27 am
Posts: 34
I haven't find gegeric way to do that with criteria, so if you can, go with hql, or do it with 2 request and merge the results. (or wait a suitable response :) )


Top
 Profile  
 
 Post subject: U can go for join in criteria.
PostPosted: Thu Mar 13, 2008 1:38 am 
Beginner
Beginner

Joined: Thu May 17, 2007 9:56 am
Posts: 21
Location: India
Hi,


Did you try with disjunction ?
It is better to use disjunction for the or conditions.
I reformed your criteria as below.

Criteria criteria = session.createCriteria("File");
criteria.createAlias("Document","Document");
criteria.createAlias("Invoice","Invoice");
criteria.add(Restriction.disjunction().add(Restriction.ilike("Document.notes","somevalue"))
.add(Restriction.ilike("Invoice.notes","somevalue")));

_________________
Kuzhali


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.