|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.annotations.immutable; |
6 | 6 |
|
7 | | -import java.util.Map; |
8 | | - |
9 | 7 | import jakarta.persistence.Entity; |
10 | 8 | import jakarta.persistence.GeneratedValue; |
11 | 9 | import jakarta.persistence.Id; |
12 | 10 | import jakarta.persistence.OneToOne; |
13 | 11 | import jakarta.persistence.PersistenceException; |
14 | | - |
15 | 12 | import org.hibernate.HibernateException; |
16 | 13 | import org.hibernate.annotations.Immutable; |
17 | | - |
18 | 14 | import org.hibernate.query.Query; |
| 15 | +import org.hibernate.testing.orm.junit.DomainModel; |
19 | 16 | import org.hibernate.testing.orm.junit.JiraKey; |
20 | | -import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; |
21 | | -import org.junit.Test; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 18 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 19 | +import org.junit.jupiter.api.Test; |
22 | 20 |
|
23 | | -import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; |
24 | | -import static org.junit.Assert.assertEquals; |
25 | | -import static org.junit.Assert.assertTrue; |
26 | | -import static org.junit.Assert.fail; |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | +import static org.assertj.core.api.Fail.fail; |
27 | 23 |
|
28 | 24 | /** |
29 | 25 | * @author Vlad Mihalcea |
30 | 26 | */ |
31 | | -@JiraKey( value = "HHH-12387" ) |
32 | | -public class ImmutableEntityUpdateQueryHandlingModeExceptionTest extends BaseNonConfigCoreFunctionalTestCase { |
33 | | - |
34 | | - @Override |
35 | | - protected Class[] getAnnotatedClasses() { |
36 | | - return new Class[] { Country.class, State.class, Photo.class, |
37 | | - ImmutableEntity.class, MutableEntity.class}; |
38 | | - } |
39 | | - |
40 | | - @Override |
41 | | - protected void addSettings(Map<String,Object> settings) { |
42 | | -// settings.put( AvailableSettings.IMMUTABLE_ENTITY_UPDATE_QUERY_HANDLING_MODE, "exception" ); |
43 | | - } |
| 27 | +@JiraKey(value = "HHH-12387") |
| 28 | +@DomainModel( |
| 29 | + annotatedClasses = { |
| 30 | + Country.class, |
| 31 | + State.class, |
| 32 | + Photo.class, |
| 33 | + ImmutableEntityUpdateQueryHandlingModeExceptionTest.ImmutableEntity.class, |
| 34 | + ImmutableEntityUpdateQueryHandlingModeExceptionTest.MutableEntity.class |
| 35 | + } |
| 36 | +) |
| 37 | +@SessionFactory |
| 38 | +public class ImmutableEntityUpdateQueryHandlingModeExceptionTest { |
44 | 39 |
|
45 | 40 | @Test |
46 | | - public void testBulkUpdate(){ |
47 | | - Country _country = doInHibernate( this::sessionFactory, session -> { |
| 41 | + public void testBulkUpdate(SessionFactoryScope scope) { |
| 42 | + Country _country = scope.fromTransaction( session -> { |
48 | 43 | Country country = new Country(); |
49 | | - country.setName("Germany"); |
50 | | - session.persist(country); |
51 | | - |
| 44 | + country.setName( "Germany" ); |
| 45 | + session.persist( country ); |
52 | 46 | return country; |
53 | 47 | } ); |
54 | 48 |
|
55 | 49 | try { |
56 | | - doInHibernate( this::sessionFactory, session -> { |
57 | | - session.createQuery("update Country set name = :name" ); |
58 | | - } ); |
59 | | - fail("Should throw PersistenceException"); |
| 50 | + scope.inTransaction( session -> |
| 51 | + session.createQuery( "update Country set name = :name" ) |
| 52 | + ); |
| 53 | + fail( "Should throw PersistenceException" ); |
60 | 54 | } |
61 | 55 | catch (PersistenceException e) { |
62 | | - assertTrue( e instanceof HibernateException ); |
63 | | - assertEquals( |
64 | | - "Error interpreting query [The query attempts to update an immutable entity: [Country] (set 'hibernate.query.immutable_entity_update_query_handling_mode' to suppress)] [update Country set name = :name]", |
65 | | - e.getMessage() |
66 | | - ); |
| 56 | + assertThat( e ).isInstanceOf( HibernateException.class ); |
| 57 | + assertThat( e.getMessage() ) |
| 58 | + .isEqualTo( |
| 59 | + "Error interpreting query [The query attempts to update an immutable entity: [Country] (set 'hibernate.query.immutable_entity_update_query_handling_mode' to suppress)] [update Country set name = :name]" ); |
67 | 60 | } |
68 | 61 |
|
69 | | - doInHibernate( this::sessionFactory, session -> { |
70 | | - Country country = session.find(Country.class, _country.getId()); |
71 | | - assertEquals( "Germany", country.getName() ); |
| 62 | + scope.inTransaction( session -> { |
| 63 | + Country country = session.find( Country.class, _country.getId() ); |
| 64 | + assertThat( country.getName() ).isEqualTo( "Germany" ); |
72 | 65 | } ); |
73 | 66 | } |
74 | 67 |
|
75 | 68 | @Test |
76 | | - @JiraKey( value = "HHH-12927" ) |
77 | | - public void testUpdateMutableWithImmutableSubSelect() { |
78 | | - doInHibernate(this::sessionFactory, session -> { |
| 69 | + @JiraKey(value = "HHH-12927") |
| 70 | + public void testUpdateMutableWithImmutableSubSelect(SessionFactoryScope scope) { |
| 71 | + scope.inTransaction( session -> { |
79 | 72 | String selector = "foo"; |
80 | | - ImmutableEntity trouble = new ImmutableEntity(selector); |
81 | | - session.persist(trouble); |
| 73 | + ImmutableEntity trouble = new ImmutableEntity( selector ); |
| 74 | + session.persist( trouble ); |
82 | 75 |
|
83 | | - MutableEntity entity = new MutableEntity(trouble, "start"); |
84 | | - session.persist(entity); |
| 76 | + MutableEntity entity = new MutableEntity( trouble, "start" ); |
| 77 | + session.persist( entity ); |
85 | 78 |
|
86 | 79 | // Change a mutable value via selection based on an immutable property |
87 | 80 | String statement = "Update MutableEntity e set e.changeable = :changeable where e.trouble.id in " + |
88 | | - "(select i.id from ImmutableEntity i where i.selector = :selector)"; |
| 81 | + "(select i.id from ImmutableEntity i where i.selector = :selector)"; |
89 | 82 |
|
90 | | - Query query = session.createQuery(statement); |
91 | | - query.setParameter("changeable", "end"); |
92 | | - query.setParameter("selector", "foo"); |
| 83 | + Query query = session.createQuery( statement ); |
| 84 | + query.setParameter( "changeable", "end" ); |
| 85 | + query.setParameter( "selector", "foo" ); |
93 | 86 | query.executeUpdate(); |
94 | 87 |
|
95 | | - session.refresh(entity); |
| 88 | + session.refresh( entity ); |
96 | 89 |
|
97 | 90 | // Assert that the value was changed. If HHH-12927 has not been fixed an exception will be thrown |
98 | 91 | // before we get here. |
99 | | - assertEquals("end", entity.getChangeable()); |
100 | | - }); |
| 92 | + assertThat( entity.getChangeable() ).isEqualTo( "end" ); |
| 93 | + } ); |
101 | 94 | } |
102 | 95 |
|
103 | 96 | @Test |
104 | | - @JiraKey( value = "HHH-12927" ) |
105 | | - public void testUpdateImmutableWithMutableSubSelect() { |
106 | | - doInHibernate(this::sessionFactory, session -> { |
| 97 | + @JiraKey(value = "HHH-12927") |
| 98 | + public void testUpdateImmutableWithMutableSubSelect(SessionFactoryScope scope) { |
| 99 | + scope.inTransaction( session -> { |
107 | 100 | String selector = "foo"; |
108 | | - ImmutableEntity trouble = new ImmutableEntity(selector); |
109 | | - session.persist(trouble); |
| 101 | + ImmutableEntity trouble = new ImmutableEntity( selector ); |
| 102 | + session.persist( trouble ); |
110 | 103 |
|
111 | | - MutableEntity entity = new MutableEntity(trouble, "start"); |
112 | | - session.persist(entity); |
| 104 | + MutableEntity entity = new MutableEntity( trouble, "start" ); |
| 105 | + session.persist( entity ); |
113 | 106 |
|
114 | 107 | // Change a mutable value via selection based on an immutable property |
115 | 108 | String statement = "Update ImmutableEntity e set e.selector = :changeable where e.id in " + |
116 | | - "(select i.id from MutableEntity i where i.changeable = :selector)"; |
| 109 | + "(select i.id from MutableEntity i where i.changeable = :selector)"; |
117 | 110 |
|
118 | 111 | try { |
119 | | - session.createQuery(statement); |
| 112 | + session.createQuery( statement ); |
120 | 113 |
|
121 | | - fail("Should have throw exception"); |
| 114 | + fail( "Should have throw exception" ); |
122 | 115 | } |
123 | 116 | catch (Exception expected) { |
124 | 117 | } |
125 | | - }); |
| 118 | + } ); |
126 | 119 | } |
127 | 120 |
|
128 | 121 | @Entity(name = "MutableEntity") |
129 | | - public static class MutableEntity{ |
| 122 | + public static class MutableEntity { |
130 | 123 |
|
131 | 124 | @Id |
132 | 125 | @GeneratedValue |
|
0 commit comments