Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Composite key classes

 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm referring to

http://www.coderanch.com/t/217911/ORM/java/JPA-mark-two-fields-Id

I wonder about two things:

1. Where do the @Column annotations go when using @IdClass and multiple @Id? Into the PK class or the entity class?

2. In general, do both, the PK and entity classes have to implement java.io.Serializable or only one?

Karsten

PS: Has anyone ever realized that entering an @ sign is a problem on German keyboard layouts (AltGr-Q), because AltGr-Q is inserting a quote here?
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1 - In to the Entity class, the IdClass is not an annotated class.

2 - Both would be a good idea. Not sure that either is required to, unless you want to serialize the objects, or use a distributed cache.
 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Sutherland wrote:1 - In to the Entity class, the IdClass is not an annotated class.


The ID class is not an annotated class? Hmmm... according to this

http://forums.java.net/jive/message.jspa?messageID=139445#139445

@Column and a few others are allowed in an ID class.

What does it mean?

Because @IdClass duplicates properties (and annotations!), it might make sense to leave the annotations in the entity class and omit them in the Id class (when using @IdClass).

But when using "@EmbeddedId MyClassId myClassId" the annotations @Id, @Column etc. HAVE to go into the ID class, right? That's because there's no other place where to put the @Column etc. ??

Karsten
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is an EmbeddedId, then the id is an Embeddable and is annotated. If it is an IdClass then the IdClass is not annotated, the fields must all be mirrored and mapped in the Entity.

 
Karsten Wutzke
Ranch Hand
Posts: 106
Hibernate MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not even that. I found the following variants:

1. @Id in entity class + @Embeddable for ID class
2. @EmbeddedId in entity class and no @Embeddable for ID class
3. @IdClass + multiple @Id for entity class and no @Embeddable for the ID class

BTW that's from the Hibernate book by Gavin King and Christian Bauer (German), chapter 8.

However, it's not what I'm asking. What I'm asking is, because the respective book doesn't give a general answer, WHERE TO PUT THE @Column annotations for the three variants above!

For 1. and 2. that book uses @AttributeOverrideS in the entity class, but why not put the @Column into the ID class, given the JPA spec. allows them plus @Basic, @Lob, @Temporal, and @Enumerated!?

For 3. you can basically choose between @AttributeOverride per property in the entity class or @Column in the ID class, because the properties are duplicated.

I would like to know what the Hibernate/JPA best practices are for this... ->???

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