• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Taken From the Java EE 5 Tutorial

 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Hello there! I'm diving into JPA, and I decided It'd be the right decision to start my training by reading the JEE 5 Tutorial. I'm kinda stuck in one of the key concepts here... Let me explain..

From the JEE 5 Tutorial
Composite primary keys must be defined in a
primary key class


From that statement I understood one must write a class(restrictions applied) in which all the fields are the Composite Key itself, am I right?

Now, among the requirements of a primary key class; I don't understand the following one(s):

[Taken from the Java EE 5 Tutorial]

A composite primary key must be represented and mapped to multiple fields or properties of the entity class, or must be represented and mapped as an embeddable class.


And...

If the class is mapped to multiple fields or properties of the entity class, the names and types of the primary key fields or properties in the primary key class must match those of the entity class.


Could someone please explain to me how these last requirements are applied to the actual Entity class?

I don't understand How does one as a programmer map the composite KEY to Multiple Fields or Properties in a given Entity class.

Thanks in advance for all the support!

Best Regards!

Jose.
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jose,

It just say that there are two ways that you can use a composite id. In both cases you need a composite primary key class, but in the first case you expose the CompositePrimaryKeyClass in the second case you just expose the fields/properties on wish the Key it's composed.



We're gonna use the same class for both of the cases



or



As you could see above, the property names must match the property names in the id key class and you have to add the @Id annotation to each of the fields in the entity class itself. More info here.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good morning Angel, I just read your reply, it was exactly what I was looking for, because I couldn't understand the Theory properly due to the lack of a descriptive example. I get it now, and I hope I can continue with my learning with no further gaps that require explanation.

Good work! and thanks for your time answering my question,

Good Luck.

Sincerely,

J�se
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic