Skip to content

Commit b11f31c

Browse files
author
trayanus1026
committed
Fix lazy loading problem with custom revision entities.
We now unproxy the revision data before trying to access its data. Closes spring-projects/spring-data-envers/issues/313
1 parent 1e29f9c commit b11f31c

File tree

4 files changed

+98
-3
lines changed

4 files changed

+98
-3
lines changed

spring-data-envers/src/main/java/org/springframework/data/envers/repository/support/EnversRevisionRepositoryImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import jakarta.persistence.EntityManager;
2525

26+
import org.hibernate.Hibernate;
2627
import org.hibernate.envers.AuditReader;
2728
import org.hibernate.envers.AuditReaderFactory;
2829
import org.hibernate.envers.DefaultRevisionEntity;
@@ -32,7 +33,6 @@
3233
import org.hibernate.envers.query.AuditEntity;
3334
import org.hibernate.envers.query.AuditQuery;
3435
import org.hibernate.envers.query.order.AuditOrder;
35-
3636
import org.springframework.data.domain.Page;
3737
import org.springframework.data.domain.PageImpl;
3838
import org.springframework.data.domain.Pageable;
@@ -200,7 +200,8 @@ RevisionMetadata<?> createRevisionMetadata() {
200200

201201
return metadata instanceof DefaultRevisionEntity //
202202
? new DefaultRevisionMetadata((DefaultRevisionEntity) metadata, revisionType) //
203-
: new AnnotationRevisionMetadata<>(metadata, RevisionNumber.class, RevisionTimestamp.class, revisionType);
203+
: new AnnotationRevisionMetadata<>(Hibernate.unproxy(metadata), RevisionNumber.class, RevisionTimestamp.class,
204+
revisionType);
204205
}
205206

206207
private static RevisionMetadata.RevisionType convertRevisionType(RevisionType datum) {

spring-data-envers/src/test/java/org/springframework/data/envers/repository/support/RepositoryIntegrationTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ void testLifeCycle() {
106106

107107
Page<Revision<Integer, License>> page = licenseRepository.findRevisions(license.id, PageRequest.of(0, 10));
108108
Revisions<Integer, License> revisions = Revisions.of(page.getContent());
109-
assertThat(revisions.getLatestRevision()).isEqualTo(it);
109+
Revision<Integer, License> latestRevision = revisions.getLatestRevision();
110+
111+
assertThat(latestRevision.getRequiredRevisionNumber()).isEqualTo(it.getRequiredRevisionNumber());
112+
assertThat(latestRevision.getEntity()).isEqualTo(it.getEntity());
110113
});
111114
}
112115

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.envers.sample;
17+
18+
import jakarta.persistence.Entity;
19+
import jakarta.persistence.GeneratedValue;
20+
import jakarta.persistence.Id;
21+
import jakarta.persistence.Transient;
22+
23+
import java.util.Date;
24+
25+
import org.hibernate.envers.RevisionEntity;
26+
import org.hibernate.envers.RevisionNumber;
27+
import org.hibernate.envers.RevisionTimestamp;
28+
29+
@Entity
30+
@RevisionEntity(CustomRevisionListener.class)
31+
public class CustomRevisionEntity {
32+
33+
@Id @GeneratedValue @RevisionNumber private int id;
34+
@RevisionTimestamp private long timestamp;
35+
private String additionalData;
36+
37+
public int getId() {
38+
return this.id;
39+
}
40+
41+
public void setId(int id) {
42+
this.id = id;
43+
}
44+
45+
@Transient
46+
public Date getRevisionDate() {
47+
return new Date(this.timestamp);
48+
}
49+
50+
public long getTimestamp() {
51+
return this.timestamp;
52+
}
53+
54+
public void setTimestamp(long timestamp) {
55+
this.timestamp = timestamp;
56+
}
57+
58+
public String getAdditionalData() {
59+
return additionalData;
60+
}
61+
62+
public void setAdditionalData(String additionalData) {
63+
this.additionalData = additionalData;
64+
}
65+
66+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.envers.sample;
17+
18+
import org.hibernate.envers.RevisionListener;
19+
20+
public class CustomRevisionListener implements RevisionListener {
21+
@Override
22+
public void newRevision(Object o) {
23+
((CustomRevisionEntity) o).setAdditionalData("some data");
24+
}
25+
}

0 commit comments

Comments
 (0)