Ilya
Ilya

Reputation: 29703

Entity with two IDs

Let's assume I have defined the following Entity:

@Entity   
class User{  

   @Id
   @GeneratedValue
   @Column(name = "DB_ID")
   private long id;

   @Id
   @Column(name = "LOGIN", unique = true)
   private String code;  

//setters, getters  
}  

Question #1.
When I am using the method .find(User.class, someId), what ID do I have to set? Should I use the long field id or the String field code? Or I can I use the long field id, and String field code at the same time?

Question #2.
If I want to use the method .merge(user), in which case will the record be updated?
case #1: - id equals DB_ID, code not equals LOGIN
case #2: - id not equals DB_ID, code equals LOGIN
case #3: - idequals DB_ID and code equals LOGIN
or any other condition?

Upvotes: 1

Views: 2496

Answers (2)

DataNucleus
DataNucleus

Reputation: 15577

Any class that has composite id (multiple Id fields) has to have its own IdClass defined (which you haven't done). An instance of the IdClass is what you then pass in to EM.find. This is easily enough found in the JPA spec

Upvotes: 5

Japan Trivedi
Japan Trivedi

Reputation: 4483

For question 1 you should only pass the long ID because it is the primary key for your table and you need to find on basis of it.

Upvotes: -1

Related Questions