66

If i want severeal Column to make up an ID.

SQL example :

CONSTRAINT [PK_NAME] PRIMARY KEY ([Column1],[Column2],[Column3])

How can i do that with a Jpa Entity class ? through columndefinition ?

just setting the id field as:

value = Column1 + Column2 + Column3 // aint working.
Anders Pedersen
  • 2,255
  • 4
  • 25
  • 49

5 Answers5

126

You need to have a class for your composite key:

public class CompositeKey implements Serializable {
    private int column1;
    private int column2;
    private int column3;
}

and then in your entity class use the @IdClass annotation:

@Entity
@IdClass(CompositeKey.class)
public class EntityExample {
    @Id
    private int column1;
    @Id
    private int column2;
    @Id
    private int column3;
    ...
    ...
}

I think this should work.

There is also the other solution that @jklee mentioned. Both work, it's a matter of preference.

starball
  • 20,030
  • 7
  • 43
  • 238
Raul Cuth
  • 2,269
  • 1
  • 12
  • 17
  • 5
    Note that there is a requirement to implement equals / hashCode (see https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#identifiers-composite) – PresentProgrammer Apr 08 '20 at 17:20
  • How would the `EntityExampleRepository extends JpaRepository< EntityExample, ???>` look like in this case? – Prasannjeet Singh Jul 31 '23 at 17:08
27

Use @Embeddable and @EmbeddedId.

Example:

@Entity
public class Project implements Serializable {
    @EmbeddedId ProjectId id;
}
 
@Embeddable
class ProjectId implements Serializable {
    int departmentId;
    long projectId;
}

More information here http://www.objectdb.com/java/jpa/entity/id#Embedded_Primary_Key_

lorraine batol
  • 6,001
  • 16
  • 55
  • 114
jklee
  • 2,198
  • 2
  • 15
  • 25
23

If all fields in the class are part of primary key, then solution would be pretty simple (extending solution provided by @raul-cuth):

@Entity
@IdClass(EntityExample.class)
public class EntityExample implements Serializable {

    @Id
    private int column1;

    @Id
    private int column2;

    @Id
    private int column3;
}
heroin
  • 2,148
  • 1
  • 23
  • 32
  • What if one of the fields is a FK? It would be a typical scenario when mapping a many-to-many *relationship* table. – Paulo Merson Dec 13 '22 at 18:28
4
  1. Using @IdClass annotation on the @Entity class followed by @Id annotation on individual fields that are part of composite primary key.
  2. Alternatively can make use of @Embeddable class which can consist of individual fields of the composite primary key and then a reference of this class can be used as an attribute with @Embedded annotation in @Entity class. Hope this helps.
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Penguin
  • 41
  • 2
2

Be aware that the hibernate Entity-Class-to-SQL-DDL-Script generator will sort all the fields, and irrespective of the order in which it appears in the definitions, will create the table definition and the index / constraint definitions in this sorted order of the fields.

While the order of appearance of the fields in the table definition may not matter much, the order of fields in a composite index definitely do. So your key-fields must be named so that when sorted by their names they are in the order you desire for the index).

Alok P
  • 994
  • 1
  • 9
  • 18