0

I am new to JPA. I am currently using JPA2.0 in WAS 8.5.5

I have a search screen where we have lot of search criteria's. Now, I have to create Query in JPA such a way that any criteria user has selected, it automatically check for that particular column in DB.

I am not able to find any solution on that. It seems to me that for every search criteria, I have to write new named Query.

Any suggestion or pointers will be appreciated.

1 Answer 1

2

You could do it in named query, but you would have to check every single possible criteria if it is null in order to avoid null values affect the results. Beware of unnecessary inner joins, each nullable relation should be included as left join

select e from Employee e left join e.department d
where (:name is null or e.name = :name)
  and (:email is null or e.email = :email)
  and (:deptId is null or d.id = :deptId)
...

However, depending on complexity of possible combinations, better option could be to not use named queries, but dynamically construct a JPQL depending on selected criteria.

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

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.