JPA Composite id. Problem referencing property in NamedQuery

How can I reference a composite id property in a named query. The following simple example does not work as I keep getting exceptions.
I am using JPA/Hibernate

The named queries are ok whenever I do not include references to the fields contained in the composite id. But I need these.

Is there a known bug related to this ?  

Below is a test code sample simulating this problem, as well as the exception stack trace

@Entity
@Table(name = "MYCUSTOMER")
@NamedQuery(name="XXXXX", query="SELECT m FROM Mycustomer m WHERE m.fname = 'JOHN' AND m.postcode ='BBBBB'")
public class Mycustomer implements java.io.Serializable {
      // Fields
      private MycustomerId id; // this is the composite key.
      private String fname;
      private String lname;
.... constructors etc.....
 
      // Property accessors
      @EmbeddedId
      @AttributeOverrides( {
                  @AttributeOverride(name = "postcode", column = @Column(name = "POSTCODE", length = 5)),
                  @AttributeOverride(name = "dob", column = @Column(name = "DOB", length = 5)) })
 
      public MycustomerId getId() {
            return this.id;
      }
 
 
}
 
 
And now the class for the composite key....
 
@Embeddable
public class MycustomerId implements java.io.Serializable {
 
      // Fields
      private String postcode;
                      private String dob;
... constructors.....
 
      // Property accessors
 
      @Column(name = "POSTCODE", length = 5)
      public String getPostcode() {
            return this.postcode;
      }
 
      public void setPostcode(String postcode) {
            this.postcode = postcode;
      }
 
}
 
Now the log listing showing what happens once app is started
 
DEBUG 17:39:21,772 (FromElement.java 108) - FromClause{level=1} :  com.mytest.db.Mycustomer (m) -> mycustomer0_
DEBUG 17:39:21,787 (FromReferenceNode.java 51) - Resolved :  m -> (mycustomer0_.DOB, mycustomer0_.POSTCODE)
DEBUG 17:39:21,787 (FromReferenceNode.java 51) - Resolved :  m -> (mycustomer0_.DOB, mycustomer0_.POSTCODE)
DEBUG 17:39:21,787 (FromElement.java 525) - handling property dereference [com.mytest.db.Mycustomer (m) -> fname (class)]
DEBUG 17:39:21,787 (DotNode.java 568) - getDataType() : fname -> org.hibernate.type.StringType@2d59a3
DEBUG 17:39:21,819 (FromReferenceNode.java 51) - Resolved :  m.fname -> mycustomer0_.fname
DEBUG 17:39:21,819 (FromReferenceNode.java 51) - Resolved :  m -> (mycustomer0_.DOB, mycustomer0_.POSTCODE)
DEBUG 17:39:21,819 (SessionFactoryImpl.java 412) - Checking 0 named SQL queries
ERROR 17:39:21,834 (SessionFactoryImpl.java 363) - Error in named query: XXXXX
org.hibernate.QueryException: could not resolve property: postcode of: com.mytest.db.Mycustomer [SELECT m FROM com.mytest.db.Mycustomer m WHERE m.fname = 'JOHN' AND m.postcode ='BBBBB']
      at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
      at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:38)
      at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1358)
      at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:279)
      at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:386)
      at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:566)
      at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:241)
      at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:188)
      at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:94)
      at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:90)
      at org.hibernate.hql.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:728)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1216)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4041)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3525)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1690)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
      at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
      at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
      at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
      at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
      at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
      at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
      at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:402)
      at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:352)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1294)
      at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:915)
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:730)
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:121)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
      at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
      at com.mytest.db.EntityManagerHelper.<clinit>(EntityManagerHelper.java:30)

broadridgeDevAsked:
Who is Participating?
 
NelliosConnect With a Mentor Commented:
Postcode is not a property of MyCustomer but a property of MyCustomerId. This is why you can't access it using m.postcode. Try something like m.id.postcode.
0
 
broadridgeDevAuthor Commented:
Thanks. Did not realize I could chain references in that way. Works perfectly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.