|
7 | 7 | import jakarta.persistence.criteria.CriteriaBuilder; |
8 | 8 | import jakarta.persistence.criteria.CriteriaUpdate; |
9 | 9 | import jakarta.persistence.criteria.Root; |
10 | | - |
11 | 10 | import org.hibernate.testing.orm.junit.DomainModel; |
| 11 | +import org.hibernate.testing.orm.junit.JiraKey; |
12 | 12 | import org.hibernate.testing.orm.junit.SessionFactory; |
13 | 13 | import org.hibernate.testing.orm.junit.SessionFactoryScope; |
14 | | -import org.junit.jupiter.api.Test; |
| 14 | +import org.hibernate.testing.orm.junit.SessionFactoryScopeAware; |
| 15 | +import org.junit.jupiter.params.ParameterizedTest; |
| 16 | +import org.junit.jupiter.params.provider.Arguments; |
| 17 | +import org.junit.jupiter.params.provider.MethodSource; |
15 | 18 |
|
16 | 19 | import java.util.Date; |
| 20 | +import java.util.function.Consumer; |
| 21 | +import java.util.stream.Stream; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
17 | 24 |
|
18 | | -@DomainModel( annotatedClasses = { Log.class } ) |
| 25 | +@DomainModel(annotatedClasses = {Log.class}) |
19 | 26 | @SessionFactory |
20 | | -public class ConvertedDateAttributeTest { |
21 | | - @Test |
22 | | - public void testConvertedFieldUpdateUsingStaticModel(SessionFactoryScope scope) { |
| 27 | +@JiraKey(value = "HHH-19862") |
| 28 | +public class ConvertedDateAttributeTest implements SessionFactoryScopeAware { |
| 29 | + private SessionFactoryScope scope; |
| 30 | + |
| 31 | + @ParameterizedTest |
| 32 | + @MethodSource("criteriaUpdateFieldSetters") |
| 33 | + public void testConvertedFieldUpdateUsingPath(Consumer<UpdateContext> criteriaUpdateFieldSetter) { |
23 | 34 |
|
24 | 35 | scope.inTransaction( (session) -> { |
| 36 | + Log log = new Log(); |
| 37 | + session.persist( log ); |
| 38 | + |
25 | 39 | final CriteriaBuilder cb = session.getCriteriaBuilder(); |
26 | 40 | final CriteriaUpdate<Log> query = cb.createCriteriaUpdate( Log.class ); |
27 | | - query.set( Log_.date, new Date()); |
28 | | - session.createMutationQuery( query ).executeUpdate(); |
| 41 | + final Root<Log> root = query.from( Log.class ); |
| 42 | + query.where( cb.equal( root.get( Log_.id ), log.getId() ) ); |
| 43 | + Date update = new Date(); |
| 44 | + criteriaUpdateFieldSetter.accept( new UpdateContext( query, root, update ) ); |
| 45 | + |
| 46 | + int updates = session.createMutationQuery( query ).executeUpdate(); |
| 47 | + session.refresh( log ); |
| 48 | + |
| 49 | + assertEquals( 1, updates ); |
| 50 | + assertEquals( log.getLastUpdate(), update ); |
| 51 | + |
29 | 52 | } ); |
| 53 | + } |
30 | 54 |
|
| 55 | + @Override |
| 56 | + public void injectSessionFactoryScope(SessionFactoryScope scope) { |
| 57 | + this.scope = scope; |
31 | 58 | } |
32 | 59 |
|
33 | | - @Test |
34 | | - public void testConvertedFieldUpdateUsingPath(SessionFactoryScope scope) { |
| 60 | + static class UpdateContext { |
| 61 | + final CriteriaUpdate<Log> query; |
| 62 | + final Root<Log> root; |
| 63 | + final Date lastUpdate; |
35 | 64 |
|
36 | | - scope.inTransaction( (session) -> { |
37 | | - final CriteriaBuilder cb = session.getCriteriaBuilder(); |
38 | | - final CriteriaUpdate<Log> query = cb.createCriteriaUpdate( Log.class ); |
39 | | - final Root<Log> root = query.from( Log.class ); |
40 | | - query.set(root.get(Log_.date), new Date()); |
41 | | - session.createMutationQuery( query ).executeUpdate(); |
42 | | - } ); |
| 65 | + public UpdateContext(CriteriaUpdate<Log> query, Root<Log> root, Date lastUpdate) { |
| 66 | + this.query = query; |
| 67 | + this.root = root; |
| 68 | + this.lastUpdate = lastUpdate; |
| 69 | + } |
| 70 | + } |
43 | 71 |
|
| 72 | + static Stream<Arguments> criteriaUpdateFieldSetters() { |
| 73 | + Consumer<UpdateContext> updateUsingPath = context -> |
| 74 | + context.query.set( context.root.get( Log_.lastUpdate ), context.lastUpdate ); |
| 75 | + Consumer<UpdateContext> updateUsingSingularAttribute = context -> |
| 76 | + context.query.set( Log_.lastUpdate, context.lastUpdate ); |
| 77 | + Consumer<UpdateContext> updateUsingName = context -> |
| 78 | + context.query.set( Log_.LAST_UPDATE, context.lastUpdate ); |
| 79 | + return Stream.of( |
| 80 | + Arguments.of( updateUsingPath ), |
| 81 | + Arguments.of( updateUsingSingularAttribute ), |
| 82 | + Arguments.of( updateUsingName ) |
| 83 | + ); |
44 | 84 | } |
45 | 85 | } |
0 commit comments