Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate query for multiple items in a collection
    primarykey
    data
    text
    <p>I have a data model that looks something like this:</p> <pre><code>public class Item { private List&lt;ItemAttribute&gt; attributes; // other stuff } public class ItemAttribute { private String name; private String value; } </code></pre> <p>(this obviously simplifies away a lot of the extraneous stuff)</p> <p>What I want to do is create a query to ask for all Items with one OR MORE particular attributes, ideally joined with arbitrary ANDs and ORs. Right now I'm keeping it simple and just trying to implement the AND case. In pseudo-SQL (or pseudo-HQL if you would), it would be something like:</p> <pre><code>select all items where attributes contains(ItemAttribute(name="foo1", value="bar1")) AND attributes contains(ItemAttribute(name="foo2", value="bar2")) </code></pre> <p>The examples in the Hibernate docs didn't seem to address this particular use case, but it seems like a fairly common one. The disjunction case would also be useful, especially so I could specify a list of possible values, i.e.</p> <pre><code>where attributes contains(ItemAttribute(name="foo", value="bar1")) OR attributes contains(ItemAttribute(name="foo", value="bar2")) -- etc. </code></pre> <p>Here's an example that works OK for a single attribute:</p> <pre><code>return getSession().createCriteria(Item.class) .createAlias("itemAttributes", "ia") .add(Restrictions.conjunction() .add(Restrictions.eq("ia.name", "foo")) .add(Restrictions.eq("ia.attributeValue", "bar"))) .list(); </code></pre> <p>Learning how to do this would go a long ways towards expanding my understanding of Hibernate's potential. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload