Guto Marrara Marzagao
Guto Marrara Marzagao

Reputation: 342

Hibernate: filter by list using criteria

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?

Upvotes: 4

Views: 6728

Answers (1)

Ean V
Ean V

Reputation: 5439

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.

Upvotes: 5

Related Questions