Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to implement three OR criterion using Hibernate?

Posted on 2008-10-18
1
Medium Priority
?
300 Views
Last Modified: 2013-12-29
There has to be a simple explaination for this, but I am trying to create a query using the createCriteria method that has three "OR" conditions.

e.g. Select * from table where field=x or field=y or field=z

I can do it for two driteria, but what about three??  (See code below - sepcifically the line where there is a "<------"
// create the or expressions for open transaction statuses
	        Criterion active = Restrictions.eq("xactionStatusLookup.xactionStatusId",1);
	        Criterion option = Restrictions.eq("xactionStatusLookup.xactionStatusId",2);
	        Criterion pending = Restrictions.eq("xactionStatusLookup.xactionStatusId",3);
	        LogicalExpression orExp = Restrictions.or(active,option,pending);  <---- **** THIS ONLY TAKES 2 PARAMETERS
			
			List<Xaction> results = (List<Xaction>) sessionFactory
				.getCurrentSession()
				.createCriteria("com.rre.model.Xaction")
					.add(orExp)
					.add(Expression.eq("xactionStatusLookup.xactionStatusId",2))
					.setFetchMode("contact", FetchMode.JOIN)
					.addOrder( Order.asc("propStreet") )
					.addOrder( Order.asc("propNum") )
					.list();

Open in new window

0
Comment
Question by:burtonrhodes
1 Comment
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 22749074
I believe you can put an Or expression as one of the parameters so you chain them; however, I would recommend use of IN like:

field IN (1, 2, 3)

http://www.hibernate.org/hib_docs/v3/api/org/hibernate/criterion/InExpression.html
0

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question