sokie
sokie

Reputation: 1986

JPA query compare lists

Let's say I have an entity like this:

public class Class1 {

private long id;
    ...
private List<OneRandomObject> list;
}

is it possible in JPA with criteria query or named query to compare entities by their lists?

something like a named query:

select c from Class1 c where c.list=:list

queries using criteria api would work fine as well.

Upvotes: 3

Views: 6342

Answers (2)

carbontax
carbontax

Reputation: 2184

I am not sure if this matches your case, but won't this do what you want?

    public List<Class1> matchList(List<OneRandomObject> other) {
        // ...
        String jpql = "select c from Class1 c where c.list in (:other)"
        // ...
    }

I see what you are asking now. How about something this in Hibernate Criteria API?

    Criteria crit = session.createQuery(Class1.class);

    Conjunction junction = Restrictions.conjunction();

    for(OneRandomObject o: matches) {
        junction.add(Restrictions.propertyEq("list", o);
    }

    crit.add(junction);

Upvotes: 2

lichengwu
lichengwu

Reputation: 4307

As far as I know, it's impossible to filter the whole collection as a property.

Maybe you can try to get all Class1 along with the list by your conditions, and filter the result as below:

for(Class1 c1 : class1List)
{
    List<OneRandomObject> list = c1.getList();
    // compare the list with you param
    // remove if not match
}

Upvotes: 1

Related Questions