Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to criteria search for matching map column (MapKeyColumn) in Oracle
    primarykey
    data
    text
    <p>I have this entity and want to find any entities with the same property set.</p> <pre><code>@Entity public class PropertyResource { @ElementCollection @MapKeyColumn(name = "property_key") @Column(name = "property_value") @CollectionTable(name = "resource_properties") private Map&lt;String, String&gt; properties = Maps.newHashMap(); ... } </code></pre> <p>This is my implementation using criteria builder:</p> <pre><code>public List&lt;PropertyResource&gt; findDuplicateProperties(PropertyResource resource) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery&lt;PropertyResource&gt; query = builder.createQuery(PropertyResource.class); Root&lt;PropertyResource&gt; resourceRoot = query.from(PropertyResource.class); List&lt;Predicate&gt; clauses = Lists.newArrayList(); for (Entry&lt;String, String&gt; entry : resource.getProperties().entrySet()) { MapJoin&lt;PropertyResource, String, String&gt; properties = resourceRoot.joinMap("properties", JoinType.INNER); clauses.add(builder.and(builder.equal(properties.key(), entry.getKey()), builder.equal(properties.value(), entry.getValue()))); } if (!resource.isNew()) { clauses.add(builder.notEqual(resourceRoot.get("id"), resource.getID())); } clauses.add(builder.equal(resourceRoot.get("type"), resource.getType())); return em.createQuery(query.where(clauses.toArray(new Predicate[clauses.size()]))).getResultList(); } </code></pre> <p>I am doing an extra join per property. There are not many properties and resources with same type have the same property keys. This works great in h2 which produces this:</p> <pre><code>select ... from resource_table propertyre0_ inner join resource_properties properties1_ on propertyre0_.entid=properties1_.PropertyResource_entid inner join resource_properties properties2_ on propertyre0_.entid=properties2_.PropertyResource_entid where properties1_.property_key=? and properties1_.property_value=? and properties2_.property_key=? and properties2_.property_value=? and propertyre0_.entid&lt;&gt;4 and propertyre0_.resource_type=? </code></pre> <p>For Oracle however I get this:</p> <pre><code>select ... from resource_table propertyre0_ where propertyre0_.entid&lt;&gt;230 and propertyre0_.resource_type=? </code></pre> <p>Is there anything wrong in my query?</p> <p>OBS: Almost forgot. Hibernate version is 4.1.9.Final.</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.
    1. This table or related slice is empty.
    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