Note that there are some explanatory texts on larger screens.

plurals
  1. POjpa set where-clause in query for fields over 2 mapjoins
    primarykey
    data
    text
    <p>I'm trying to construct a complex query. My entities look as follows:</p> <pre><code>@Entity public class Configuration{ @Id @Column(name="CONF_ID") protected Long configurationId; @ManyToMany @MapKey(name="componentType") @JoinTable(name="CONF_COMP", joinColumns={@JoinColumn(name="CONF_ID", referencedColumnName="CONF_ID")}, inverseJoinColumns={@JoinColumn(name="COMP_ID", referencedColumnName="componentId")}) protected Map&lt;String, Component&gt; components; } </code></pre> <p>And </p> <pre><code>@Entity public class Component { @Id protected long componentId; @ElementCollection protected Map&lt;String, String&gt; properties; @ManyToMany(mappedBy="components") private List&lt;Configuration&gt; configurations; @Column(name="COMP_TYPE") protected String componentType; } </code></pre> <p>My problem lies in querying the <code>properties</code> field properly. I can't seem to create a query to get all Configurations where Component A has Property Prop1 = 1 and Component B has Property Prop2=2. I tried the following without success.</p> <pre><code>Root&lt;Configuration&gt; conf = cq.from(Configuration.class); MapJoin&lt;Configuration, String, Component&gt; compJoin = conf.join(Configuration_.components, JoinType.LEFT); MapJoin&lt;Component, String, String&gt; propJoin = compJoin.join(Component_.properties, JoinType.LEFT); Predicate p1 = cb.and( cb.equal(mapJoin.key(), "A"), cb.equal(propJoin.key(), "Prop1"), cb.equal(propJoin.value(), "1")); Predicate p2 = cb.and( cb.equal(mapJoin.key(), "B"), cb.equal(propJoin.key(), "Prop2"), cb.equal(propJoin.value(), "2")); Predicate[] pArray = new Predicate[]{p1, p2}; cq.where(pArray); cq.select(conf).distinct(true); </code></pre> <p>EDIT: The query, as outputted by the logger, looks like this:</p> <pre><code>SELECT DISTINCT t2.CONF_ID, t2.DTYPE, t2.TOTALPRICE, t2.NAME FROM CONFIGURATION t2 LEFT OUTER JOIN (CONF_COMP t3 JOIN COMPONENT t1 ON (t1.COMPONENTID = t3.COMP_ID)) ON (t3.CONF_ID = t2.CONF_ID) LEFT OUTER JOIN Component_PROPERTIES t0 ON (t0.Component_COMPONENTID = t1.COMPONENTID) WHERE (((((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?)) AND t0.PROPERTIES LIKE ?) AND (((t1.COMP_TYPE = ?) AND (t0.PROPERTIES_KEY = ?)) AND t0.PROPERTIES LIKE ?)) AND (t2.DTYPE = ?)) bind =&gt; [7 parameters bound] </code></pre> <p>I'm guessing it is trying to find a Configuration where all the conditions apply to the SAME component maybe? It works if I apply restrictions on only one Component, but I get an empty result list when applying 2 or more, although there are entries in the DB that satisfy the conditions. </p> <p><strong>UPDATE</strong></p> <p>After following Pimgd's suggestion, i ended up with a query that looks like this:</p> <pre><code>SELECT DISTINCT t1.CONF_ID, t1.DTYPE, t1.TOTALPRICE, t1.NAME FROM CONFIGURATION t1 LEFT OUTER JOIN (CONF_COMP t2 JOIN COMPONENT t0 ON (t0.COMPONENTID = t2.COMP_ID)) ON (t2.CONF_ID = t1.CONF_ID) WHERE ((( t0.COMPONENTID IN (SELECT t3.COMPONENTID FROM COMPONENT t3 LEFT OUTER JOIN Component_PROPERTIES t4 ON (t4.Component_COMPONENTID = t3.COMPONENTID) WHERE ((t4.PROPERTIES_KEY = Brand) AND (t4.PROPERTIES = Intel)))) AND t0.COMPONENTID IN (SELECT t6.COMPONENTID FROM COMPONENT t6 LEFT OUTER JOIN Component_PROPERTIES t7 ON (t7.Component_COMPONENTID = t6.COMPONENTID) WHERE ((t7.PROPERTIES_KEY = Capacity) AND t7.PROPERTIES LIKE 4GB%)))) </code></pre> <p>One criteria works, two yield no results.</p> <p>Any help much appreciated!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    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