Hibernate Envers - 传递自定义字段
Hibernate Envers - pass custom field
我对 Hibernate Envers 有疑问。如何将自定义字段传递给 RevisionListener?
例如:
我有一些带有@Audit 注释的实体,一切正常。但我不知道如何传递 "comment".
等自定义字段
@Entity
@Table(name = "REVINFO")
@RevisionEntity(MyRevisionEntityListener.class)
public class RevEntity {
@Id
@RevisionNumber
@Column(name = "REV", nullable = false, updatable = false)
private Integer id;
@RevisionTimestamp
@Column(name = "REVTSTMP", nullable = false, updatable = false)
private Date timestamp;
@Column(name = "MODIFIED_BY", length = 100)
private String modifiedBy;
@Column(name = "COMMENT", length = 100)
private String comment;
public class MyRevisionEntityListener implements RevisionListener {
@Override
public void newRevision(Object revisionEntity) {
RevEntity a = (RevEntity) revisionEntity;
a.setComment("");//how can i populate it?
}
}
那么,我该如何设置评论区呢?
编辑--------
我发现了 ThreadLocal 的一些技巧 - 但它是安全的 "active users" ?
例如:
public class CustomComment {
public static final CustomComment INSTANCE = new CustomComment();
private static final ThreadLocal<String> storage = new ThreadLocal<>();
public void setComment(String comment) {
storage.set(comment);
}
public String get() {
return storage.get();
}
}
我有修改实体的服务:
CustomComment.INSTANCE.setComment("custom comment");
someRepository.save(someEntity);
一切都如我所愿。
但这种解决方案安全吗?如果有 2 个或更多人 "change" 不同的实体,这会有效吗?
Than everything works how i want. But is this solution safe? If 2 or
more ppl "change" different entities, will this work good?
如果您在 @Transaction
内执行更新,那么事务边界内的所有内容都将在同一个线程中发生,所以一切都很好。
我对 Hibernate Envers 有疑问。如何将自定义字段传递给 RevisionListener? 例如:
我有一些带有@Audit 注释的实体,一切正常。但我不知道如何传递 "comment".
等自定义字段@Entity
@Table(name = "REVINFO")
@RevisionEntity(MyRevisionEntityListener.class)
public class RevEntity {
@Id
@RevisionNumber
@Column(name = "REV", nullable = false, updatable = false)
private Integer id;
@RevisionTimestamp
@Column(name = "REVTSTMP", nullable = false, updatable = false)
private Date timestamp;
@Column(name = "MODIFIED_BY", length = 100)
private String modifiedBy;
@Column(name = "COMMENT", length = 100)
private String comment;
public class MyRevisionEntityListener implements RevisionListener {
@Override
public void newRevision(Object revisionEntity) {
RevEntity a = (RevEntity) revisionEntity;
a.setComment("");//how can i populate it?
}
}
那么,我该如何设置评论区呢?
编辑--------
我发现了 ThreadLocal 的一些技巧 - 但它是安全的 "active users" ?
例如:
public class CustomComment {
public static final CustomComment INSTANCE = new CustomComment();
private static final ThreadLocal<String> storage = new ThreadLocal<>();
public void setComment(String comment) {
storage.set(comment);
}
public String get() {
return storage.get();
}
}
我有修改实体的服务:
CustomComment.INSTANCE.setComment("custom comment");
someRepository.save(someEntity);
一切都如我所愿。 但这种解决方案安全吗?如果有 2 个或更多人 "change" 不同的实体,这会有效吗?
Than everything works how i want. But is this solution safe? If 2 or more ppl "change" different entities, will this work good?
如果您在 @Transaction
内执行更新,那么事务边界内的所有内容都将在同一个线程中发生,所以一切都很好。