Hibernate 实体的具体修订参考

Hibernate 实体的具体修订参考,hibernate,hibernate-envers,Hibernate,Hibernate Envers,我有一个实体引用了另一个实体,类似这样: @Entity(name = "MyCustomEntity") @Table(name = "my_custom_entity") public class MyCustomEntity { @Id Integer id; @ManyToOne @JoinColumn(name = "profile_id") OtherEntity other; } 事实证明,业务逻辑规定,OtherEntity在此上下文

我有一个实体引用了另一个实体,类似这样:

@Entity(name = "MyCustomEntity")
@Table(name = "my_custom_entity")
public class MyCustomEntity {

    @Id
    Integer id;

    @ManyToOne
    @JoinColumn(name = "profile_id")
    OtherEntity other;
}
事实证明,业务逻辑规定,
OtherEntity
在此上下文中不应该是可变的。由于您仍然可以在该上下文之外更改实体,因此我认为将引用从
MyCustomEntity
更改为Envers修订版是最容易的

我可以用以下代码段替换引用:

@Entity(name = "MyCustomEntity")
@Table(name = "my_custom_entity")
public class MyCustomEntity {

    @Id
    Integer id;

    Integer otherId;
    Integer revision;
}

OtherEntity getOther(MyCustomEntity entity) throws Exception {
   return auditReader.find(OtherEntity.class, entity.otherId, entity.revision);
}
但是我会失去一些我更愿意拥有的Hibernate特性


有没有更好的方法来引用Envers修订版?

基于您只是想对两个实体之间的关系强制执行不变性的事实,为什么不在这里简单地使用
@Immutable
注释,而不是试图通过您的模型来操作它呢

@Entity(name = "MyCustomEntity")
@Table(name = "my_custom_entity")
public class MyCustomEntity {
  @Id
  private Integer id;

  @ManyToOne
  @JoinColumn(name = "profile_id")
  @Immutable
  private OtherEntity other;

  ...
}
但重要的是要理解,本内容中的
不可变
仅适用于ORM

假设您执行以下操作

  • 创建指向其他实体的
    MyCustomEntity
    修订版
  • 修改
    OtherEntity
    (现在的修订号高于
    MyCustomEntity
  • 修改
    MyCustomEntity
    (现在的修订号高于
    OtherEntity
  • 查询最新版本的MyCustomEntity
  • 最新版本指向
    其他实体
    ,并在步骤2中进行修改。不变性注释不允许更改FK引用

    这能解决你的问题吗

    更新

    我希望
    MyCustomEntity
    始终指向同一版本的
    OtherEntity
    。因此,即使当
    OtherEntity
    更改时,
    MyCustomEntity
    也不应该能够看到更改

    这正是Envers的工作原理

    MyCustomEntity
    仅返回版本号等于或小于其查询版本的相关关联

    这意味着,如果修改
    OtherEntity
    ,然后查询
    MyCustomEntity
    ,则返回的
    OtherEntity
    实例将不包含这些最近的更改

    MyCustomEntity
    返回
    OtherEntity
    关联的最新快照的唯一时间是
    MyCustomEntity
    在同一事务中或在
    OtherEntity
    之后的未来事务中被修改


    关联总是与具有修订号的关联实体类型一起返回,而修订号不是
    ,这是因为您只是想对两个实体之间的关系强制执行不变性,为什么不在这里简单地使用
    @Immutable
    注释,而不是试图通过您的模型对此进行操作呢

    @Entity(name = "MyCustomEntity")
    @Table(name = "my_custom_entity")
    public class MyCustomEntity {
      @Id
      private Integer id;
    
      @ManyToOne
      @JoinColumn(name = "profile_id")
      @Immutable
      private OtherEntity other;
    
      ...
    }
    
    但重要的是要理解,本内容中的
    不可变
    仅适用于ORM

    假设您执行以下操作

  • 创建指向其他实体的
    MyCustomEntity
    修订版
  • 修改
    OtherEntity
    (现在的修订号高于
    MyCustomEntity
  • 修改
    MyCustomEntity
    (现在的修订号高于
    OtherEntity
  • 查询最新版本的MyCustomEntity
  • 最新版本指向
    其他实体
    ,并在步骤2中进行修改。不变性注释不允许更改FK引用

    这能解决你的问题吗

    更新

    我希望
    MyCustomEntity
    始终指向同一版本的
    OtherEntity
    。因此,即使当
    OtherEntity
    更改时,
    MyCustomEntity
    也不应该能够看到更改

    这正是Envers的工作原理

    MyCustomEntity
    仅返回版本号等于或小于其查询版本的相关关联

    这意味着,如果修改
    OtherEntity
    ,然后查询
    MyCustomEntity
    ,则返回的
    OtherEntity
    实例将不包含这些最近的更改

    MyCustomEntity
    返回
    OtherEntity
    关联的最新快照的唯一时间是
    MyCustomEntity
    在同一事务中或在
    OtherEntity
    之后的未来事务中被修改


    关联总是与相关实体类型一起返回,其修订号大于
    ,因此我的主要目标是使
    MyCustomEntity
    OtherEntity
    之间的关系不可变,以便一旦将
    OtherEntity#1234
    关联到
    MyCustomEntity#789
    它不能被改变?@Naros完全正确。那么我的主要目标是使
    MyCustomEntity
    OtherEntity
    之间的关系不可变,这样一旦你将
    OtherEntity#1234
    MyCustomEntity#789
    关联,它就不能被改变了。不,我好像误解了。我希望
    MyCustomEntity
    始终指向同一版本的
    OtherEntity
    。因此,即使当
    OtherEntity
    更改时,
    MyCustomEntity
    也不应该能够看到更改。FK引用也是不可变的,但这不是主要问题。仍然感谢您的时间。您是在更改
    MyCustomEntity
    还是仅更改
    OtherEntity
    ?如果只有后者,那么
    MyCustomEntity
    无论如何都不会看到这些更改。我正在同时更改这两个。不,我似乎误解了。我希望
    MyCustomEntity
    始终指向同一版本的
    OtherEntity
    。因此,即使当
    OtherEntity
    更改时,
    MyCustomEntity
    也不应该能够看到更改。