wood burning stoves*
The moose likes Object Relational Mapping and the fly likes Problem finding objets with composite key 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 "Problem finding objets with composite key" Watch "Problem finding objets with composite key" New topic
Author

Problem finding objets with composite key

noemi conde
Greenhorn

Joined: May 05, 2010
Posts: 14
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

Joined: Jul 20, 2010
Posts: 106

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


OCJP JavaSE 6 (86%)
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem finding objets with composite key
 
Similar Threads
How to map ManyToOne relationship with Composite key?
Why is my Hibernate Search query not working?
ManyToMany relationship with extra field in the joinTable
@PrimaryKeyJoinColumn: no insertable and no updatable
ManyToOne Composite Key with Transient Field