Entity


Composite Primary Key
An entity with a composite primary key that is composed of 2 or more entity properties requires a primary key class. The properties in the primary key class will match the entity properties that make up the primary key. The primary key class has to be serializable and must have a no argument constructor. This class is referenced from the entity through the @IdClass annotation.

Here’s an example of an entity with a composite primary key:

@Entity
@IdClass(value = LocalCategoryPK.class)
public class LocalCategory implements Serializable {
	private static final long serialVersionUID = -8647726205620015818L;
	@Id
	private int locationId;
	@Id
	private String categoryCode;

	public LocalCategory() {
	}
...
}

The primary key class LocalCategoryPK is just a POJO:

public class LocalCategoryPK implements Serializable {
    private static final long serialVersionUID = 2686130969945396040L;
    private int locationId;
    private String categoryCode;

    private LocalCategoryPK() {
    }

// getters and setters

// hashcode and equals override

}

You must be logged in to post a comment.