4

I have a Doctor entity. Each doctor has a list of HealthInsurance which is another entity.

My goal is to make a search system based on the HealthInsurance's the user select. I have to get all doctors that has at least 1 HealthInsurance contained in the user health insurance's list. Something like this:

Criteria criteria = session.createCriteria(Doctor.class);
criteria.add(Restrictions.in("healthInsurances", userHealthInsurances));
return criteria.list();

But this code is throwing the following exception: java.sql.SQLException: No value specified for parameter 1.

Is there any way of doing this using Criteria?

1 Answer 1

5
Criteria criteria = session.createCriteria(Doctor.class);
criteria.createCriteria("healthInsurances").add(Restrictions.in("id", userSelectedIds));
return criteria.list();

You may get the same functionality through createAlias. Have a look at this for more examples.

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

4 Comments

It worked, thanks. As I reveice a list of entities instead of a list of ids, is there any way to perform the same operation without having to generate the list of ids?
It might work if you overwrite the equals and hashCode methods of your HealthInsurance object with its id and pass userHealthInsurances list as the parameter of in method; but not sure about this ;)
You mean like I propose in the question? When I do this I get the error I said (java.sql.SQLException: No value specified for parameter 1). And if keep what you said but changing "userSelectedIds" to "userHealthInsurances" I get the following error: java.lang.ClassCastException: HealthInsurance cannot be cast to Long.
I meant the second one(that you got ClassCastException) and turns out that it doesn't work.

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.