File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem finding objets with composite key" Watch "Problem finding objets with composite key" New topic

Problem finding objets with composite key

noemi conde

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.


OCJP JavaSE 6 (86%)
Karsten Wutzke
Ranch Hand

Joined: Jul 20, 2010
Posts: 106


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

I agree. Here's the link:
subject: Problem finding objets with composite key
jQuery in Action, 3rd edition