【问题标题】:EclipseLink equivalent of Hibernate @NaturalIDEclipseLink 相当于 Hibernate @NaturalID
【发布时间】:2015-10-13 13:41:25
【问题描述】:

直到今天我还没有使用 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;
}

【问题讨论】:

  • 您能否提供您实体的 sn-p 以便我们确定使用要求?

标签: java hibernate eclipselink


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 2013-07-23
      • 2016-03-31
      • 1970-01-01
      • 2022-07-31
      • 2011-03-15
      • 2015-11-07
      • 2023-03-31
      相关资源
      最近更新 更多