3

We are looking to to conditionally add where clauses to a SQL where class

For example we have a DAO that has a method with say 10 params.

For each of those params we check if it is null, if not we add an AND to the where clause.

The "base" query is a hard coded string and we concat it with the ANDS.

I'm looking for ideas for a more elegent way of doing this.

We are using hibernate elsewhere in the app

3 Answers 3

2

You can use the Hibernate criteria API to dynamically build queries.

Sign up to request clarification or add additional context in comments.

Comments

1

For simplicity you can use variable argument method and start a loop for array and check for not null and concat it. otherwise you can use the Hibernate criteria API.

Comments

0

The Hibernate Criteria might be what you want.

http://www.mkyong.com/hibernate/hibernate-criteria-examples/

http://www.dil.univ-mrs.fr/~massat/docs/hibernate-3.1/api/org/hibernate/Criteria.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.