Fix lazy loading problem with custom revision entities.

We now unproxy the revision data before trying to access its data.

Closes #313
This commit is contained in:
Jens Schauder
2021-11-03 15:12:30 +01:00
parent 1062b0966f
commit f152b6c1b4
4 changed files with 101 additions and 3 deletions

View File

@@ -23,6 +23,7 @@ import java.util.Optional;
import javax.persistence.EntityManager;
import org.hibernate.Hibernate;
import org.hibernate.envers.AuditReader;
import org.hibernate.envers.AuditReaderFactory;
import org.hibernate.envers.DefaultRevisionEntity;
@@ -32,7 +33,6 @@ import org.hibernate.envers.RevisionType;
import org.hibernate.envers.query.AuditEntity;
import org.hibernate.envers.query.AuditQuery;
import org.hibernate.envers.query.order.AuditOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
@@ -208,7 +208,8 @@ public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N
return metadata instanceof DefaultRevisionEntity //
? new DefaultRevisionMetadata((DefaultRevisionEntity) metadata, revisionType) //
: new AnnotationRevisionMetadata<>(metadata, RevisionNumber.class, RevisionTimestamp.class, revisionType);
: new AnnotationRevisionMetadata<>(Hibernate.unproxy(metadata), RevisionNumber.class, RevisionTimestamp.class,
revisionType);
}
private static RevisionMetadata.RevisionType convertRevisionType(RevisionType datum) {

View File

@@ -106,7 +106,10 @@ class RepositoryIntegrationTests {
Page<Revision<Integer, License>> page = licenseRepository.findRevisions(license.id, PageRequest.of(0, 10));
Revisions<Integer, License> revisions = Revisions.of(page.getContent());
assertThat(revisions.getLatestRevision()).isEqualTo(it);
Revision<Integer, License> latestRevision = revisions.getLatestRevision();
assertThat(latestRevision.getRequiredRevisionNumber()).isEqualTo(it.getRequiredRevisionNumber());
assertThat(latestRevision.getEntity()).isEqualTo(it.getEntity());
});
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.envers.sample;
import org.hibernate.envers.DefaultRevisionEntity;
import org.hibernate.envers.RevisionEntity;
import org.hibernate.envers.RevisionNumber;
import org.hibernate.envers.RevisionTimestamp;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.util.Date;
@Entity
@RevisionEntity(CustomRevisionListener.class)
public class CustomRevisionEntity {
@Id
@GeneratedValue
@RevisionNumber
private int id;
@RevisionTimestamp
private long timestamp;
private String additionalData;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Transient
public Date getRevisionDate() {
return new Date(this.timestamp);
}
public long getTimestamp() {
return this.timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public String getAdditionalData() {
return additionalData;
}
public void setAdditionalData(String additionalData) {
this.additionalData = additionalData;
}
}

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.envers.sample;
import org.hibernate.envers.RevisionListener;
public class CustomRevisionListener implements RevisionListener {
@Override
public void newRevision(Object o) {
((CustomRevisionEntity) o).setAdditionalData("some data");
}
}