java EclipseLink相当于Hibernate@naturaid
直到今天我才开始使用EclipseLink,我似乎找不到一个@NaturalId
。那么,在日食中,冬眠的@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;
}
# 1 楼答案
@cacheindex允许您通过自然id而不是定义的@id进行搜索,从而使jpa查询从二级缓存命中中受益。这当然只会使单结果查询受益。如果您想获得结果列表查询的缓存命中率,甚至是没有索引的查询,可以在eclipselink中使用查询缓存
# 2 楼答案
看着https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/annotations/NaturalId.html @naturaid注释似乎是一种在DDL生成过程中指定表约束的方法
如果是这样,JPA实现这一点的方法是在表定义的注释中直接使用@UniqueConstraint(columnNames={“TASKKEY”})定义约束,如下所示: JPA - defining multi-column unique constraints
JPA不允许将映射标记为不可变,尽管可以将映射中的字段标记为insertable=false、Updateable=false。EclipseLink还有一个@Mutable注释,如下所述: https://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/a_mutable.htm