This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes JPA composite keys problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA composite keys problem" Watch "JPA composite keys problem" New topic
Author

JPA composite keys problem

Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
hi guys, I'm new in JPA
I have a table with composite key in my DB
in related entity bean I use @IdClass annotation for composite keys, (1:is it cool? what's the difference between @IdClass & @EmbeddedId?)

well in composite keys related class I override boolean equals(Object obj) method
but seems I should override int hashcode() method too
i know a little about hashcode

and problem
how do I override this guy?
I mean what is it for?

code for composite keys class


Open source
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3060
    
  38

what's the difference between @IdClass & @EmbeddedId?


ArAsh,

The @EmbeddedId is used to annotate an ID field of an entity, which has a separate composite class, as below:
(Where NameAndAge is a separate class which is annotated with the @Embeddable annotation).

In the above case, you can simply pass an NameAndAge object to set the primary key when it is needed.


The IdClass method is used to include multiple @Id fields to the same entity

(Where NameAndAgePK is a POJO class)

In this case, the above POJO class is used to 'evaluate' the primary keys before associating them with the entity.



i know a little about hashcode

and problem
how do I override this guy?
I mean what is it for?


You might know that hashcode is a numerical representation of the identity of an object; hence the business logic of overriding it is dependent on your requirements, and the class specifications.



Note: I feel your business logic for the equals(Object) method is too long. You can implement it as below, in a nice way:



Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Zaeed McColin
Ranch Hand

Joined: Jan 13, 2009
Posts: 90
Ba Sepas (thank you) Mr. Cooray
as i understood we use @IdClass annotation when we want for example check some condition, evaluate or something like that in our composite key

well if I put evaluating on DB(MySQL), I mean check the correct and legal record for adding in table
is it necessary to check and evaluating again with JPA again?(I mean use @IdClass for keys)
can I use @EmbeddedId instead of it?

Ba Sepas
 
 
subject: JPA composite keys problem