Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

JPA composite keys problem

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 6951
1317
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:


 
Jason Marlyn
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic