Search code examples
javahibernateeclipselink

EclipseLink equivalent of Hibernate @NaturalID


I haven't worked until today with EclipseLink and I don't seem to able to find a @NaturalId. So what is the equivalent of Hibernate's @NaturalId in EclipseLink ?

import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.NaturalId;

import lombok.Data;

@Entity
@Data
public class Task {

enum Status{DONE,IN_PROGRESS,OPEN,REOPENED}

@Id
private long id;
@NaturalId
private String taskKey;
private Status status;
private int initEstimation;
@Embedded
Progress progress;
}

Solution

  • Looking at https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/annotations/NaturalId.html the @NaturalId annotation seems to be a way to specify constraints on the table to be used during DDL generation.

    If so, the JPA way to accomplish the same is to define the constraints yourself directly using @UniqueConstraint(columnNames = {"TASKKEY"}) within the annotation for the table definition, as shown here: JPA - defining multi-column unique constraints

    JPA does not allow marking mappings immutable, though you can mark the field within a map as insertable=false, updatable=false. EclipseLink also has an @Mutable annotation described here: https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/a_mutable.htm