简体   繁体   中英

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;
}

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

@cacheindex allows you to benefit jpa queries from obtaining L2 cache hits when search is by natural ids and not the defined @id. This ofcourse will benefit single result queries only. If you want to obtain cache hits for result list queries or even queries without indexes, you can go for query cache in eclipselink

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM