Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem finding objets with composite key

 
noemi conde
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchies !!!
I've got a problem dealing with composite primary keys in hibernate


Here is the code:




We have a composite primary key with objects from two different schemas, and we don't know how to do the mapping.

The exception thrown when we try to execute this hql query: "Select A from Table_A" is



Do you know what is the problem ???

Any idea is welcome, thanks in advance.

 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your mapping is completely wrong. Every entity class that has a composite primary key must have its own ID class, so if you have 2 tables, this will result in 4 classes, 2 entity classes and 2 ID classes.

There are 4 variants of how to declare these:

JPA 1.0 @IdClass
JPA 1.0 @EmbeddedId
JPA 2.0 @IdClass
JPA 2.0 @EmbeddedId

whichever you choose does matter, because JPA implementations (Hibernate, EclipseLink, ...) have a different degree of bugginess with these.

I made a lot of tests and to me the JPA 1.0 @IdClass has the fewest problems. The Hibernate mapping system is very instable and throws many unexpected exceptions, so you might want to try EclipseLink or another JPA provider.

Don't expect ORMs to do what you think. Be prepared.

As for your code, please pick up a good source on how to map composite key classes. I don't have time to correct completely incorrect code.

Karsten
 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See http://stackoverflow.com/questions/96729/what-are-the-best-books-for-hibernate-jpa

Forget "Java Persistence with Hibernate"! "Pro JPA 2" by Mike Keith is a thousand times better. The former is very bad with composite keys.

Also see http://en.wikibooks.org/wiki/Java_Persistence

Karsten
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic