JPA Composite id. Problem referencing property in NamedQuery

Posted on 2009-02-13
Last Modified: 2012-05-06
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>(

Question by:broadridgeDev
    LVL 10

    Accepted Solution

    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

    Author Closing Comment

    Thanks. Did not realize I could chain references in that way. Works perfectly.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Title # Comments Views Activity
    format the code in java 6 60
    matchUp  challenge 6 35
    twoTwo  challenge 35 71
    Receive file in Servlet 1 27
    I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    The viewer will learn how to implement Singleton Design Pattern in Java.
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now