Hibernate query for multiple items in a collection

后端 未结 5 625
灰色年华
灰色年华 2021-02-14 01:24

I have a data model that looks something like this:

public class Item {
    private List attributes;
    // other stuff
}

public class Item         


        
5条回答
  •  别那么骄傲
    2021-02-14 01:47

    Why wouldn't the following work?

    return getSession().createCriteria(Item.class)
        .createAlias("itemAttributes", "ia")
        .add(Restrictions.or()
            .add(Restrictions.conjunction()
                .add(Restrictions.eq("ia.name", "foo1"))
                .add(Restrictions.eq("ia.attributeValue", "bar1")))
            .add(Restrictions.conjunction()
                .add(Restrictions.eq("ia.name", "foo2"))
                .add(Restrictions.eq("ia.attributeValue", "bar2"))))
        .list();
    

    That would be (name=foo1 && attributeValue=bar1) OR (name=foo2 && attributeValue=bar2)

提交回复
热议问题