Solved

How to implement three OR criterion using Hibernate?

Posted on 2008-10-18
1
291 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 59

Accepted Solution

by:
Kevin Cross earned 500 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SHA2 certs for IIS AND Java? 2 122
ejb mdb examples 1 19
How to log java errors in tomcat 8 24
Strange router problem - can't access hotmail.com 14 50
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

749 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