Solved

How to implement three OR criterion using Hibernate?

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
How to execute a Python program and gather return output in Java 2 40
String array comparison 4 38
restrict decimal places for double datatype 10 19
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 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