我有一个实体,我想跟踪所有更改,因此我创建了新实体进行审核。 以下是我的主要实体:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "primary")
public class PrimaryEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "primary_id")
private Long id;
private String name;
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection
@CollectionTable(
name = "primary_attachments",
joinColumns = @JoinColumn(name = "primary_id")
)
private List<String> attachments;
@CreatedDate
@Temporal(TemporalType.DATE)
private Date createDate;
@LastModifiedDate
@Temporal(TemporalType.DATE)
private Date lastModifiedDate;
}
以下是我的审计实体:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "primary_audit")
public class PrimaryEntityAudit {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "audit_id")
private Long id;
@NotNull
@Column(name = "primary_entity_id")
private Long primaryId;
private String name;
@LazyCollection(LazyCollectionOption.FALSE)
@ElementCollection
@CollectionTable(
name = "primary_attachments_audit",
joinColumns = @JoinColumn(name = "primary_entity_id")
)
private List<String> attachments = new ArrayList<>();
@CreatedDate
@Temporal(TemporalType.DATE)
private Date createDate;
public PrimaryEntityAudit(PrimaryEntity primaryEntity) {
this.primaryId = primaryEntity.getId();
this.attachments.addAll(primaryEntity.getAttachments());
this.createDate = new Date();
}
}
在更新主要实体之前,我创建新的PrimaryEntityAudit并保存此对象,然后更新主要实体。 并且操作成功并保存对象PrimaryEntityAudit,但不保存来自PrimaryEntityAudit的附件。
我也在ProjectEntityAudit的构造函数中尝试了setAttachments,但后来我得到了一个例外:HibernateExcpetion:找到了对集合的共享引用。
如何映射审核集合以保存旧的PrimaryEntity附件状态?