| 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
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2701
|
|
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 (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- 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
|
|
|