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

@Table(name = "MYCUSTOMER")
@NamedQuery(name="XXXXX", query="SELECT m FROM Mycustomer m WHERE m.fname = 'JOHN' AND m.postcode ='BBBBB'")
public class Mycustomer implements {
      // Fields
      private MycustomerId id; // this is the composite key.
      private String fname;
      private String lname;
.... constructors etc.....
      // Property accessors
      @AttributeOverrides( {
                  @AttributeOverride(name = "postcode", column = @Column(name = "POSTCODE", length = 5)),
                  @AttributeOverride(name = "dob", column = @Column(name = "DOB", length = 5)) })
      public MycustomerId getId() {
And now the class for the composite key....
public class MycustomerId implements {
      // 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 ( 108) - FromClause{level=1} :  com.mytest.db.Mycustomer (m) -> mycustomer0_
DEBUG 17:39:21,787 ( 51) - Resolved :  m -> (mycustomer0_.DOB, mycustomer0_.POSTCODE)
DEBUG 17:39:21,787 ( 51) - Resolved :  m -> (mycustomer0_.DOB, mycustomer0_.POSTCODE)
DEBUG 17:39:21,787 ( 525) - handling property dereference [com.mytest.db.Mycustomer (m) -> fname (class)]
DEBUG 17:39:21,787 ( 568) - getDataType() : fname -> org.hibernate.type.StringType@2d59a3
DEBUG 17:39:21,819 ( 51) - Resolved :  m.fname -> mycustomer0_.fname
DEBUG 17:39:21,819 ( 51) - Resolved :  m -> (mycustomer0_.DOB, mycustomer0_.POSTCODE)
DEBUG 17:39:21,819 ( 412) - Checking 0 named SQL queries
ERROR 17:39:21,834 ( 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(
      at org.hibernate.persister.entity.AbstractPropertyMapping.toType(
      at org.hibernate.persister.entity.AbstractEntityPersister.toType(
      at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(
      at org.hibernate.hql.ast.tree.FromElement.getPropertyType(
      at org.hibernate.hql.ast.tree.DotNode.getDataType(
      at org.hibernate.hql.ast.tree.DotNode.prepareLhs(
      at org.hibernate.hql.ast.tree.DotNode.resolve(
      at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(
      at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(
      at org.hibernate.hql.ast.HqlSqlWalker.resolve(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.expr(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.exprOrSubquery(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(
      at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(
      at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(
      at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(
      at org.hibernate.hql.ast.QueryTranslatorImpl.compile(
      at org.hibernate.engine.query.HQLQueryPlan.<init>(
      at org.hibernate.engine.query.HQLQueryPlan.<init>(
      at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(
      at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(
      at org.hibernate.impl.SessionFactoryImpl.<init>(
      at org.hibernate.cfg.Configuration.buildSessionFactory(
      at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(
      at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(
      at javax.persistence.Persistence.createEntityManagerFactory(
      at javax.persistence.Persistence.createEntityManagerFactory(
      at com.mytest.db.EntityManagerHelper.<clinit>(

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
broadridgeDevAuthor Commented:
Thanks. Did not realize I could chain references in that way. Works perfectly.
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.