Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Entity manager: Hibernate generating wrong SQL (composite key of composite keys) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Entity manager: Hibernate generating wrong SQL (composite key of composite keys)" Watch "Entity manager: Hibernate generating wrong SQL (composite key of composite keys)" New topic
Author

Entity manager: Hibernate generating wrong SQL (composite key of composite keys)

Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

Hello,

I'm using a table that has four columns as primary key, with pairs of two columns each referencing another table via composite FK (MySQL 5.1). Both references are identifying relationships, that is they are PK as well as FK in the PlayerStats table. The design can be viewed here:
http://www.kawoolutions.com/media/hibernate-wrong-sql-playerstats.jpg

Here's the stack trace (root cause):



The generated SQL is:


As you can see from the generated SQL, the first two columns in the SELECT are the names of the two composite primary key classes: "playerstat0_.score" and "playerstat0_.teamMember". They're also used in the WHERE clause for whatever reason. The code is obviously wrong. Can't say whether there's something wrong in my code or Hibernate.

Here's the Java code for the PlayerStat and PlayerStatId classes:



Note that I omitted the imports and the equals and hashCode methods. Note also this is supposed to be JPA 1.0 code. However I believe that doesn't matter much in terms of code generation.

In the servlet I simply call:


Can anyone tell what might go wrong?

Karsten


OCJP JavaSE 6 (86%)
 
 
subject: Entity manager: Hibernate generating wrong SQL (composite key of composite keys)
 
Similar Threads
Entity bean construction: flattened constructors for composite primary key classes
Hibernate JPQL/HQL: aggregate functions show results of wrong table/entity joined twice
@PrimaryKeyJoinColumn: no insertable and no updatable
Named query Exception
Queries and "column not found" error