15
15
*/
16
16
package org .springframework .data .envers .repository .support ;
17
17
18
- import static org .springframework .data .history .RevisionMetadata .RevisionType .*;
19
-
20
- import java .util .ArrayList ;
21
- import java .util .List ;
22
- import java .util .Optional ;
23
-
24
- import javax .persistence .EntityManager ;
25
-
26
18
import org .hibernate .Hibernate ;
27
19
import org .hibernate .envers .AuditReader ;
28
20
import org .hibernate .envers .AuditReaderFactory ;
32
24
import org .hibernate .envers .RevisionType ;
33
25
import org .hibernate .envers .query .AuditEntity ;
34
26
import org .hibernate .envers .query .AuditQuery ;
27
+ import org .hibernate .envers .query .criteria .AuditProperty ;
35
28
import org .hibernate .envers .query .order .AuditOrder ;
36
29
import org .springframework .data .domain .Page ;
37
30
import org .springframework .data .domain .PageImpl ;
49
42
import org .springframework .transaction .annotation .Transactional ;
50
43
import org .springframework .util .Assert ;
51
44
45
+ import javax .persistence .EntityManager ;
46
+ import java .util .ArrayList ;
47
+ import java .util .Collections ;
48
+ import java .util .List ;
49
+ import java .util .Optional ;
50
+
51
+ import static org .springframework .data .history .RevisionMetadata .RevisionType .*;
52
+
52
53
/**
53
54
* Repository implementation using Hibernate Envers to implement revision specific query methods.
54
55
*
@@ -72,12 +73,12 @@ public class EnversRevisionRepositoryImpl<T, ID, N extends Number & Comparable<N
72
73
* Creates a new {@link EnversRevisionRepositoryImpl} using the given {@link JpaEntityInformation},
73
74
* {@link RevisionEntityInformation} and {@link EntityManager}.
74
75
*
75
- * @param entityInformation must not be {@literal null}.
76
+ * @param entityInformation must not be {@literal null}.
76
77
* @param revisionEntityInformation must not be {@literal null}.
77
- * @param entityManager must not be {@literal null}.
78
+ * @param entityManager must not be {@literal null}.
78
79
*/
79
80
public EnversRevisionRepositoryImpl (JpaEntityInformation <T , ?> entityInformation ,
80
- RevisionEntityInformation revisionEntityInformation , EntityManager entityManager ) {
81
+ RevisionEntityInformation revisionEntityInformation , EntityManager entityManager ) {
81
82
82
83
Assert .notNull (revisionEntityInformation , "RevisionEntityInformation must not be null!" );
83
84
@@ -145,29 +146,47 @@ public Revisions<N, T> findRevisions(ID id) {
145
146
146
147
147
148
private AuditOrder mapRevisionSort (RevisionSort revisionSort ) {
148
- return RevisionSort .getRevisionDirection (revisionSort ).isDescending () //
149
- ? AuditEntity .revisionNumber ().desc () //
150
- : AuditEntity .revisionNumber ().asc ();
149
+
150
+ return RevisionSort .getRevisionDirection (revisionSort ).isDescending () //
151
+ ? AuditEntity .revisionNumber ().desc () //
152
+ : AuditEntity .revisionNumber ().asc ();
151
153
}
152
154
153
- private AuditOrder mapPropertySort (Sort sort ) {
154
- return sort .stream ().findFirst ().map (order -> order .getDirection ().isAscending () ?
155
- AuditEntity .property (order .getProperty ()).asc () :
156
- AuditEntity .property (order .getProperty ()).desc ())
157
- .orElse (AuditEntity .revisionNumber ().asc ());
155
+ private List <AuditOrder > mapPropertySort (Sort sort ) {
156
+
157
+ if (sort .isEmpty ()) {
158
+ return Collections .singletonList (AuditEntity .revisionNumber ().asc ());
159
+ }
160
+
161
+ List <AuditOrder > result = new ArrayList <>();
162
+ for (Sort .Order order : sort ) {
163
+
164
+ AuditProperty <Object > property = AuditEntity .property (order .getProperty ());
165
+ AuditOrder auditOrder = order .getDirection ().isAscending () ?
166
+ property .asc () :
167
+ property .desc ();
168
+
169
+ result .add (auditOrder );
170
+ }
171
+
172
+ return result ;
158
173
}
159
174
160
175
@ SuppressWarnings ("unchecked" )
161
176
public Page <Revision <N , T >> findRevisions (ID id , Pageable pageable ) {
162
- AuditOrder orderMapped = (pageable .getSort () instanceof RevisionSort ) ?
163
- mapRevisionSort ((RevisionSort ) pageable .getSort ()) :
164
- mapPropertySort (pageable .getSort ());
165
177
166
- List <Object []> resultList = createBaseQuery (id ) //
167
- .addOrder (orderMapped ) //
168
- .setFirstResult ((int ) pageable .getOffset ()) //
169
- .setMaxResults (pageable .getPageSize ()) //
170
- .getResultList ();
178
+ AuditQuery baseQuery = createBaseQuery (id );
179
+
180
+ List <AuditOrder > orderMapped = (pageable .getSort () instanceof RevisionSort ) ?
181
+ Collections .singletonList (mapRevisionSort ((RevisionSort ) pageable .getSort ())) :
182
+ mapPropertySort (pageable .getSort ());
183
+
184
+ orderMapped .forEach (baseQuery ::addOrder );
185
+
186
+ List <Object []> resultList = baseQuery //
187
+ .setFirstResult ((int ) pageable .getOffset ()) //
188
+ .setMaxResults (pageable .getPageSize ()) //
189
+ .getResultList ();
171
190
172
191
Long count = (Long ) createBaseQuery (id ) //
173
192
.addProjection (AuditEntity .revisionNumber ().count ()).getSingleResult ();
@@ -224,7 +243,7 @@ RevisionMetadata<?> createRevisionMetadata() {
224
243
return metadata instanceof DefaultRevisionEntity //
225
244
? new DefaultRevisionMetadata ((DefaultRevisionEntity ) metadata , revisionType ) //
226
245
: new AnnotationRevisionMetadata <>(Hibernate .unproxy (metadata ), RevisionNumber .class , RevisionTimestamp .class ,
227
- revisionType );
246
+ revisionType );
228
247
}
229
248
230
249
private static RevisionMetadata .RevisionType convertRevisionType (RevisionType datum ) {
0 commit comments