I joined a project where they implemented Springboot v.2.0.5
, Hibernate + Envers v5.3.7
. My job is now to generate logs for a nice little endpoint.
When I try to do this utilizing the auditreader through the following code:
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);
}
it works pretty well for normal elements such as id, name etc. Problem is, that every join column is not correctly initialized and throw
'org.hibernate.ObjectNotFoundException' exception. Cannot evaluate classPath.class$HibernateProxy$YUt19z4c.toString()
A sample of the entity looks like this:
@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;
I have not implemented any custom RevisionEntities
, and the tables revision entities where generated manually by hibernate. Here are the DDLs.
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),
);
I tried to follow the guides from: https://docs.jboss.org/hibernate/orm/5.3/userguide/html_single/Hibernate_User_Guide.html#envers-schema
Similar question was asked in: Hibernate Envers: Initializing Envers Proxies but using a modelMapper did not solve the issue for me.
Otherwise I searched the whole internet to day, so any help will be appreciated.