Example of the creation of a composite primary key using JPA
In this example,we create a composite primary key in two differents ways
- using @IdClass
- using @Embeddable
Defining the AccountId class and the attributes to be used as composite primary key
Defining the Account class, adding @IdClass(AccountId.class) and the @Id attributes(must be the same as AccountId)
Defining the BookId class and the attributes to be used as composite primary key, the BookId class is annotated with @Embeddable
Defining the Book class and the bookId attribute to be used as composite primary key, the bookId attribute is annotated with @EmbeddedId
Defining the Load class to create account and book instances