Skip to content

Commit f152b6c

Browse files
committed
Fix lazy loading problem with custom revision entities.
We now unproxy the revision data before trying to access its data. Closes #313
1 parent 1062b09 commit f152b6c

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed

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 javax.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;
@@ -208,7 +208,8 @@ RevisionMetadata<?> createRevisionMetadata() {
208208

209209
return metadata instanceof DefaultRevisionEntity //
210210
? new DefaultRevisionMetadata((DefaultRevisionEntity) metadata, revisionType) //
211-
: new AnnotationRevisionMetadata<>(metadata, RevisionNumber.class, RevisionTimestamp.class, revisionType);
211+
: new AnnotationRevisionMetadata<>(Hibernate.unproxy(metadata), RevisionNumber.class, RevisionTimestamp.class,
212+
revisionType);
212213
}
213214

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

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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.DefaultRevisionEntity;
19+
import org.hibernate.envers.RevisionEntity;
20+
import org.hibernate.envers.RevisionNumber;
21+
import org.hibernate.envers.RevisionTimestamp;
22+
23+
import javax.persistence.Entity;
24+
import javax.persistence.GeneratedValue;
25+
import javax.persistence.Id;
26+
import javax.persistence.Transient;
27+
import java.util.Date;
28+
29+
@Entity
30+
@RevisionEntity(CustomRevisionListener.class)
31+
public class CustomRevisionEntity {
32+
33+
@Id
34+
@GeneratedValue
35+
@RevisionNumber
36+
private int id;
37+
@RevisionTimestamp
38+
private long timestamp;
39+
private String additionalData;
40+
41+
public int getId() {
42+
return this.id;
43+
}
44+
45+
public void setId(int id) {
46+
this.id = id;
47+
}
48+
49+
@Transient
50+
public Date getRevisionDate() {
51+
return new Date(this.timestamp);
52+
}
53+
54+
public long getTimestamp() {
55+
return this.timestamp;
56+
}
57+
58+
public void setTimestamp(long timestamp) {
59+
this.timestamp = timestamp;
60+
}
61+
public String getAdditionalData() {
62+
return additionalData;
63+
}
64+
65+
public void setAdditionalData(String additionalData) {
66+
this.additionalData = additionalData;
67+
}
68+
69+
}
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)