wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 3011
    
  34

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:



Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
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
 
Consider Paul's rocket mass heater.
 
subject: JPA composite keys problem
 
Similar Threads
The hashCode() e equals() methods was overriden
Override equals and hashCode
Problem with refresh() method of EntityManamger in JPA
ArrayList.Contains always return true, even the objects are not equal
hashCode() & equals() method test