我加入了一个项目,他们在其中实施了Springboot v.2.0.5,Hibernate + Envers v5.3.7。我现在的工作是为一个不错的小端点生成日志。当我尝试通过以下代码利用auditreader进行此操作时:
public List<Drug> getDrugAudByID(long drugId) {
try (Session session = hibernateSession.openSession()) {
AuditReader auditReader = AuditReaderFactory.get(session);
List<Number> revisions = AuditReaderFactory.get(session).getRevisions(Drug.class, drugId);
List<Drug> drugVersions = new ArrayList<>();
for (Number rev : revisions) {
Drug drug = auditReader.find(Drug.class, drugId, rev);
drugVersions.add(drug);
}
它对于id,name等普通元素非常有效。问题是,每个连接列均未正确初始化,并引发““ org.hibernate.ObjectNotFoundException”异常。无法评估classPath.class $ HibernateProxy $ YUt19z4c.toString()“
实体示例如下:
@Entity
@Audited
@Table(name = "drugs")
@Check(constraints = "id <> genericDrug")
public class Drug {
public static String genericPrefix = "73019";
public static String specificPrefix = "73010";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "drugId", unique = true)
private BigDecimal drugId;
@Column(name = "partType")
private String partType;
@Column(name = "name")
private String name;
@OneToOne()
@JoinColumn(name = "form", referencedColumnName = "id")
private FormType form;
我尚未实现任何自定义RevisionEntities,并且表的修订版本实体是由休眠手动生成的。这是DDL。
create table REVINFO
(
REV int auto_increment
primary key,
REVTSTMP bigint null
);
#Only a sample of the true DLL, but encapsulates the problematic form Field
create table drugs_AUD
(
REV int not null,
REVTYPE tinyint null,
id bigint not null,
drugId decimal(11) null,
partType varchar(2) null,
name varchar(30) null,
form bigint null,
substancesComplete bit default b'0' null,
primary key (id, REV),
constraint fk_drugs_REV
foreign key (REV) references REVINFO (REV),
);
类似的问题在:Hibernate Envers: Initializing Envers Proxies但是使用modelMapper无法为我解决问题。
否则,我今天会搜索整个互联网,因此将不胜感激。
我找到了原因。
数据是在项目开始时使用SQL转储插入的,这意味着它绕过了休眠和常规客户端站点执行。结果是表单和其他实体的AUD表已过时。
由于以前没有使用过修订历史,所以我的解决方案是:
TRUNCATE TABLE drugs_AUD;
TRUNCATE magi.REVINFO;
INSERT INTO magi.REVINFO (REV, REVTSTMP) VALUES (1, UNIX_TIMESTAMP());
#Showing an example for on AUD table below
INSERT INTO magi.drugs_AUD (REV, REVTYPE, id,
drugId, partType, name, form, substancesComplete)
SELECT 1, 0, id, drugId, partType, name, form, substancesComplete from drugs;