Solved

How to implement three OR criterion using Hibernate?

Posted on 2008-10-18
1
283 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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now