Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use single column from composite key in jpaql or hql query
    primarykey
    data
    text
    <p>Provider is hibernate 3.6.</p> <p>I have a composite key declared via @IdClass, consisting of 4 columns. Everything works fine when it comes to storing and retrieving entity instances via a complete primary key. However, if I wish to query for all entities that have a particular value in just a single column, hibernate throws exceptions.</p> <pre><code>@Entity @IdClass(MyBeanId.class) public class MyBean { @Id private Integer partId1; @Id private Integer partId2; @Id private Integer partId3; @Id private String partId4; private Integer otherProperty1; private Double otherProperty2; private Double otherProperty3; private String otherProperty4; /* removed getters and setters */ } </code></pre> <p>query code in another class:</p> <pre><code>public List&lt;MyBean&gt; findByPartId1(Integer partId1) { return em.createQuery("select b from MyBean b where b.partId1 = :partId1", MyBean.class).setParameter("partId1", partId1).getResultList(); } </code></pre> <p>Another query which is failing, with an error message about not knowing the type of a parameter is as follows:</p> <pre><code>public List&lt;Integer&gt; getAllPartId1s() { return em.createQuery("select distinct b.partId1 from MyBean b", Integer.class).getResultList(); } </code></pre> <p>The exception I'm getting from the first query is as follows:</p> <pre><code>Caused by: org.hibernate.QueryException: could not resolve property: partId1 of: some.company.entity.MyBean [select t from some.company.entity.MyBean b where b.partId1 = :partId1] at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:81) at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:75) at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1465) at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:315) at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:487) at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:611) at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:263) at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:210) at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117) at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113) at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:880) at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1330) at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4471) at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3944) </code></pre> <p>Surely, there is some way to access the individual columns of a composite key in a query without resorting to an @Embeddable id object? Otherwise, why even have the @IdClass mechanism, since it is all but useless. I've tried numerous variants, such as "where b.id.partId1 = :partId1" but they all fail.</p> <p>And yes, I have a MyBeanId class with the same properties and a valid hashCode and equals, but there is only so much code I want to sanitize in order to post a question.</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.
    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