30
30
31
31
import javax .persistence .EntityManager ;
32
32
33
+ import org .hibernate .envers .RevisionType ;
34
+ import org .hibernate .envers .enhanced .SequenceIdRevisionEntity ;
33
35
import org .hibernate .envers .query .AuditEntity ;
34
36
import org .hibernate .envers .query .criteria .AuditDisjunction ;
35
37
import org .hibernate .envers .test .BaseEnversJPAFunctionalTestCase ;
42
44
43
45
/**
44
46
* @author Adam Warski (adam at warski dot org)
47
+ * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
45
48
*/
46
49
@ SuppressWarnings ({"unchecked" })
47
50
public class MaximalizePropertyQuery extends BaseEnversJPAFunctionalTestCase {
48
51
Integer id1 ;
49
52
Integer id2 ;
50
53
Integer id3 ;
54
+ Integer id4 ;
51
55
52
56
@ Override
53
57
protected Class <?>[] getAnnotatedClasses () {
@@ -64,14 +68,17 @@ public void initData() {
64
68
StrIntTestEntity site1 = new StrIntTestEntity ("a" , 10 );
65
69
StrIntTestEntity site2 = new StrIntTestEntity ("b" , 15 );
66
70
StrIntTestEntity site3 = new StrIntTestEntity ("c" , 42 );
71
+ StrIntTestEntity site4 = new StrIntTestEntity ("d" , 52 );
67
72
68
73
em .persist (site1 );
69
74
em .persist (site2 );
70
75
em .persist (site3 );
76
+ em .persist (site4 );
71
77
72
78
id1 = site1 .getId ();
73
79
id2 = site2 .getId ();
74
80
id3 = site3 .getId ();
81
+ id4 = site4 .getId ();
75
82
76
83
em .getTransaction ().commit ();
77
84
@@ -107,6 +114,12 @@ public void initData() {
107
114
site2 .setStr1 ("a" );
108
115
109
116
em .getTransaction ().commit ();
117
+
118
+ // Revision 5
119
+ em .getTransaction ().begin ();
120
+ site4 = em .find ( StrIntTestEntity .class , id4 );
121
+ em .remove ( site4 );
122
+ em .getTransaction ().commit ();
110
123
}
111
124
112
125
@ Test
@@ -170,4 +183,32 @@ public void testMaximizeInDisjunction() {
170
183
}
171
184
}
172
185
}
186
+
187
+ @ Test
188
+ @ TestForIssue (jiraKey = "HHH-7827" )
189
+ public void testAllLatestRevisionsOfEntityType () {
190
+ List result = getAuditReader ().createQuery ()
191
+ .forRevisionsOfEntity ( StrIntTestEntity .class , false , true )
192
+ .add ( AuditEntity .revisionNumber ().maximize ().correlateSubquery () )
193
+ .addOrder ( AuditEntity .property ( "id" ).asc () )
194
+ .getResultList ();
195
+
196
+ Assert .assertEquals ( 4 , result .size () );
197
+
198
+ Object [] result1 = (Object []) result .get ( 0 );
199
+ Object [] result2 = (Object []) result .get ( 1 );
200
+ Object [] result3 = (Object []) result .get ( 2 );
201
+ Object [] result4 = (Object []) result .get ( 3 );
202
+
203
+ checkRevisionData ( result1 , 4 , RevisionType .MOD , new StrIntTestEntity ( "d" , 5 , id1 ) );
204
+ checkRevisionData ( result2 , 4 , RevisionType .MOD , new StrIntTestEntity ( "a" , 20 , id2 ) );
205
+ checkRevisionData ( result3 , 1 , RevisionType .ADD , new StrIntTestEntity ( "c" , 42 , id3 ) );
206
+ checkRevisionData ( result4 , 5 , RevisionType .DEL , new StrIntTestEntity ( null , null , id4 ) );
207
+ }
208
+
209
+ private void checkRevisionData (Object [] result , int revision , RevisionType type , StrIntTestEntity entity ) {
210
+ Assert .assertEquals ( entity , result [0 ] );
211
+ Assert .assertEquals ( revision , ( (SequenceIdRevisionEntity ) result [1 ] ).getId () );
212
+ Assert .assertEquals ( type , result [2 ] );
213
+ }
173
214
}
0 commit comments