[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JPA Composite id. Problem referencing property in NamedQuery

Posted on 2009-02-13
2
Medium Priority
?
4,429 Views
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

@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)

0
Comment
Question by:broadridgeDev
2 Comments
 
LVL 10

Accepted Solution

by:
Nellios earned 1000 total points
ID: 23648880
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
 

Author Closing Comment

by:broadridgeDev
ID: 31546811
Thanks. Did not realize I could chain references in that way. Works perfectly.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

831 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