|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.annotations.subselect; |
6 | 6 |
|
7 | | -import org.junit.Assert; |
8 | | -import org.junit.Test; |
9 | | - |
10 | | -import org.hibernate.Session; |
11 | | -import org.hibernate.Transaction; |
12 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
13 | 7 |
|
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
14 | 11 | import org.hibernate.type.StandardBasicTypes; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
15 | 15 |
|
16 | 16 | /** |
17 | 17 | * @author Sharath Reddy |
18 | 18 | */ |
19 | | -public class SubselectTest extends BaseCoreFunctionalTestCase { |
20 | | - @Test |
21 | | - public void testSubselectWithSynchronize() { |
22 | | - |
23 | | - Session s = openSession(); |
24 | | - Transaction tx = s.beginTransaction(); |
25 | | - |
26 | | - //We don't use auto-generated ids because these seem to cause the session to flush. |
27 | | - //We want to test that the session flushes because of the 'synchronize' annotation |
28 | | - long itemId = 1; |
29 | | - Item item = new Item(); |
30 | | - item.setName("widget"); |
31 | | - item.setId(itemId); |
32 | | - s.persist(item); |
33 | | - |
34 | | - Bid bid1 = new Bid(); |
35 | | - bid1.setAmount(100.0); |
36 | | - bid1.setItemId(itemId); |
37 | | - bid1.setId(1); |
38 | | - s.persist(bid1); |
39 | | - |
40 | | - Bid bid2 = new Bid(); |
41 | | - bid2.setAmount(200.0); |
42 | | - bid2.setItemId(itemId); |
43 | | - bid2.setId(2); |
44 | | - s.persist(bid2); |
45 | | - |
46 | | - //Because we use 'synchronize' annotation, this query should trigger session flush |
47 | | - var query = s.createQuery("from HighestBid b where b.name = :name", HighestBid.class); |
48 | | - query.setParameter( "name", "widget", StandardBasicTypes.STRING ); |
49 | | - HighestBid highestBid = query.list().iterator().next(); |
50 | | - |
51 | | - Assert.assertEquals( 200.0, highestBid.getAmount(), 0.01 ); |
52 | | - tx.rollback(); |
53 | | - s.close(); |
54 | | - } |
55 | | - |
56 | | - @Override |
57 | | - protected Class<?>[] getAnnotatedClasses() { |
58 | | - return new Class[]{ |
| 19 | +@DomainModel( |
| 20 | + annotatedClasses = { |
59 | 21 | Item.class, |
60 | 22 | Bid.class, |
61 | 23 | HighestBid.class |
62 | | - }; |
| 24 | + } |
| 25 | +) |
| 26 | +@SessionFactory |
| 27 | +public class SubselectTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testSubselectWithSynchronize(SessionFactoryScope scope) { |
| 31 | + //We don't use auto-generated ids because these seem to cause the session to flush. |
| 32 | + //We want to test that the session flushes because of the 'synchronize' annotation |
| 33 | + scope.inTransaction( |
| 34 | + session -> { |
| 35 | + long itemId = 1; |
| 36 | + Item item = new Item(); |
| 37 | + item.setName( "widget" ); |
| 38 | + item.setId( itemId ); |
| 39 | + session.persist( item ); |
| 40 | + |
| 41 | + Bid bid1 = new Bid(); |
| 42 | + bid1.setAmount( 100.0 ); |
| 43 | + bid1.setItemId( itemId ); |
| 44 | + bid1.setId( 1 ); |
| 45 | + session.persist( bid1 ); |
| 46 | + |
| 47 | + Bid bid2 = new Bid(); |
| 48 | + bid2.setAmount( 200.0 ); |
| 49 | + bid2.setItemId( itemId ); |
| 50 | + bid2.setId( 2 ); |
| 51 | + session.persist( bid2 ); |
| 52 | + |
| 53 | + //Because we use 'synchronize' annotation, this query should trigger session flush |
| 54 | + var query = session.createQuery( "from HighestBid b where b.name = :name", HighestBid.class ); |
| 55 | + query.setParameter( "name", "widget", StandardBasicTypes.STRING ); |
| 56 | + HighestBid highestBid = query.list().iterator().next(); |
| 57 | + |
| 58 | + assertEquals( 200.0, highestBid.getAmount(), 0.01 ); |
| 59 | + } |
| 60 | + ); |
63 | 61 | } |
64 | 62 |
|
65 | 63 | } |
0 commit comments