Search code examples
jpapersistenceeclipselinkcomposite-primary-key

Composite Primary key in JPA


The EmbeddedId or IdClass annotation is used to denote a composite primary key. How can i use composite primary key without ( EmbeddedId or IdClass ) ?

If it is possible to use composite primary key without ( EmbeddedId or IdClass ) then how can i use EntityManager.find( Entity Class , Object primaryKey) method to find entity in case of composite primary key(Multiple Primarykey) (because of no IdClass or EmbeddedId) .

EclipseLink take List of pk in the find() operation but if composite pk key defined in example -

    Entity Person {
           @Id 
           String username;
           @Id
           String emailId;
           @Basic
           String firstName;
           @Basic
           String lastName;
    }

    List list = new ArrayList();
    list.add(${username});  //Run time value
    list.add(${emailId});   //Run time value

then EnitityManager.find(list) will take these arguments , is i am right?

If i am assuming correct then how will EnitityManager.find() operation will know that List 1st argument is username or emailId pk value (means sequence of composite pk fields value)


Solution

  • You should be able to use a List with the find() operation in EclipseLink.