EclipseLink 相当于 Hibernate @NaturalID

EclipseLink equivalent of Hibernate @NaturalID

直到今天我才开始使用 EclipseLink,我似乎找不到 @NaturalId。那么 EclipseLink 中 Hibernate 的 @NaturalId 相当于什么?

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

正在查看https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/annotations/NaturalId.html @NaturalId 注释似乎是一种指定在 DDL 生成期间使用的 table 约束的方法。

如果是这样,JPA 方法是在 table 定义的注释中使用 @UniqueConstraint(columnNames = {"TASKKEY"}) 直接自己定义约束,如图所示这里: JPA - defining multi-column unique constraints

JPA 不允许标记映射 immutable,但您可以将映射中的字段标记为 insertable=false、updatable=false。 EclipseLink 也有一个 @Mutable 注释,描述如下: https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/a_mutable.htm

@cacheindex 允许您在通过自然 ID 而非定义的 @id 进行搜索时从获取 L2 缓存命中中受益。这当然只会有利于单一结果查询。如果你想获得结果列表查询甚至没有索引的查询的缓存命中,你可以去eclipselink

查询缓存