|
6 | 6 |
|
7 | 7 | import from.In; |
8 | 8 | import in.from.Any; |
9 | | - |
10 | | -import org.hibernate.Session; |
11 | 9 | import org.hibernate.query.Query; |
12 | | -import org.hibernate.resource.transaction.spi.TransactionStatus; |
13 | | - |
14 | | -import org.junit.Test; |
15 | | - |
| 10 | +import org.hibernate.testing.orm.junit.DomainModel; |
16 | 11 | import org.hibernate.testing.orm.junit.JiraKey; |
17 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 13 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 14 | +import org.junit.jupiter.api.AfterEach; |
| 15 | +import org.junit.jupiter.api.Test; |
18 | 16 |
|
19 | 17 | /** |
20 | 18 | * @author Andrea Boriero |
21 | 19 | */ |
22 | 20 | @JiraKey(value = "HHH-10953") |
23 | | -public class UpdateEntitiesWithPackageNamesStartingWithKeywordsTest extends BaseCoreFunctionalTestCase { |
24 | | - @Override |
25 | | - protected Class<?>[] getAnnotatedClasses() { |
26 | | - return new Class[] {Any.class, In.class}; |
| 21 | +@DomainModel( |
| 22 | + annotatedClasses = { |
| 23 | + Any.class, In.class |
| 24 | + } |
| 25 | +) |
| 26 | +@SessionFactory |
| 27 | +public class UpdateEntitiesWithPackageNamesStartingWithKeywordsTest { |
| 28 | + |
| 29 | + |
| 30 | + @AfterEach |
| 31 | + public void tearDown(SessionFactoryScope scope) { |
| 32 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
27 | 33 | } |
28 | 34 |
|
29 | 35 | @Test |
30 | | - public void testUpdateEntityWithPackageNameStartingWithIn() { |
| 36 | + public void testUpdateEntityWithPackageNameStartingWithIn(SessionFactoryScope scope) { |
31 | 37 | Any entity = new Any(); |
32 | 38 | entity.setProp( "1" ); |
33 | | - try (Session session = openSession()) { |
34 | 39 |
|
35 | | - session.getTransaction().begin(); |
36 | | - try { |
37 | | - session.persist( entity ); |
38 | | - session.getTransaction().commit(); |
39 | | - } |
40 | | - catch (Exception e) { |
41 | | - if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { |
42 | | - session.getTransaction().rollback(); |
43 | | - } |
44 | | - throw e; |
45 | | - } |
46 | | - } |
47 | | - try (Session session = openSession()) { |
48 | | - session.getTransaction().begin(); |
49 | | - try { |
50 | | - final Query query = session.createQuery( "UPDATE Any set prop = :prop WHERE id = :id " ); |
51 | | - query.setParameter( "prop", "1" ); |
52 | | - query.setParameter( "id", entity.getId() ); |
53 | | - query.executeUpdate(); |
54 | | - session.getTransaction().commit(); |
55 | | - } |
56 | | - catch (Exception e) { |
57 | | - if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { |
58 | | - session.getTransaction().rollback(); |
59 | | - } |
60 | | - throw e; |
61 | | - } |
62 | | - } |
63 | | - try (Session session = openSession()) { |
64 | | - session.getTransaction().begin(); |
65 | | - try { |
66 | | - session.createQuery( "DELETE FROM Any" ).executeUpdate(); |
67 | | - session.getTransaction().commit(); |
68 | | - } |
69 | | - catch (Exception e) { |
70 | | - if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { |
71 | | - session.getTransaction().rollback(); |
| 40 | + scope.inTransaction( |
| 41 | + session -> session.persist( entity ) |
| 42 | + ); |
| 43 | + |
| 44 | + scope.inTransaction( |
| 45 | + session -> { |
| 46 | + final Query query = session.createQuery( "UPDATE Any set prop = :prop WHERE id = :id " ); |
| 47 | + query.setParameter( "prop", "1" ); |
| 48 | + query.setParameter( "id", entity.getId() ); |
| 49 | + query.executeUpdate(); |
72 | 50 | } |
73 | | - throw e; |
74 | | - } |
75 | | - } |
| 51 | + ); |
| 52 | + scope.inTransaction( |
| 53 | + session -> session.createQuery( "DELETE FROM Any" ).executeUpdate() |
| 54 | + ); |
76 | 55 | } |
77 | 56 |
|
78 | 57 | @Test |
79 | | - public void testUpdateEntityWithPackageNameStartingWithFrom() { |
| 58 | + public void testUpdateEntityWithPackageNameStartingWithFrom(SessionFactoryScope scope) { |
80 | 59 | In entity = new In(); |
81 | 60 | entity.setProp( "1" ); |
82 | | - try (Session session = openSession()) { |
83 | | - |
84 | | - session.getTransaction().begin(); |
85 | | - try { |
86 | | - session.persist( entity ); |
87 | | - session.getTransaction().commit(); |
88 | | - } |
89 | | - catch (Exception e) { |
90 | | - if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { |
91 | | - session.getTransaction().rollback(); |
92 | | - } |
93 | | - throw e; |
94 | | - } |
95 | | - } |
96 | | - try (Session session = openSession()) { |
97 | | - session.getTransaction().begin(); |
98 | | - try { |
99 | | - final Query query = session.createQuery( "UPDATE In set prop = :prop WHERE id = :id " ); |
100 | | - query.setParameter( "prop", "1" ); |
101 | | - query.setParameter( "id", entity.getId() ); |
102 | | - query.executeUpdate(); |
103 | | - session.getTransaction().commit(); |
104 | | - } |
105 | | - catch (Exception e) { |
106 | | - if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { |
107 | | - session.getTransaction().rollback(); |
| 61 | + scope.inTransaction( |
| 62 | + session -> session.persist( entity ) |
| 63 | + ); |
| 64 | + scope.inTransaction( |
| 65 | + session -> { |
| 66 | + final Query query = session.createQuery( "UPDATE In set prop = :prop WHERE id = :id " ); |
| 67 | + query.setParameter( "prop", "1" ); |
| 68 | + query.setParameter( "id", entity.getId() ); |
| 69 | + query.executeUpdate(); |
108 | 70 | } |
109 | | - throw e; |
110 | | - } |
111 | | - } |
112 | | - try (Session session = openSession()) { |
113 | | - session.getTransaction().begin(); |
114 | | - try { |
115 | | - session.createQuery( "DELETE FROM In" ).executeUpdate(); |
116 | | - session.getTransaction().commit(); |
117 | | - } |
118 | | - catch (Exception e) { |
119 | | - if ( session.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { |
120 | | - session.getTransaction().rollback(); |
121 | | - } |
122 | | - throw e; |
123 | | - } |
124 | | - } |
| 71 | + ); |
| 72 | + |
| 73 | + scope.inTransaction( |
| 74 | + session -> session.createQuery( "DELETE FROM In" ).executeUpdate() |
| 75 | + ); |
125 | 76 | } |
126 | 77 | } |
0 commit comments