|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.entitymode.map.basic; |
6 | 6 |
|
| 7 | +import org.hibernate.Hibernate; |
| 8 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 9 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 10 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
7 | 13 | import java.util.ArrayList; |
8 | 14 | import java.util.HashMap; |
9 | 15 | import java.util.Iterator; |
10 | 16 | import java.util.List; |
11 | 17 | import java.util.Map; |
12 | 18 |
|
13 | | -import org.hibernate.Hibernate; |
14 | | -import org.hibernate.Session; |
15 | | -import org.hibernate.Transaction; |
16 | | -import org.hibernate.cfg.Configuration; |
17 | | - |
18 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
19 | | -import org.junit.Test; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
20 | 23 |
|
21 | | -import static org.junit.Assert.assertEquals; |
22 | | -import static org.junit.Assert.assertFalse; |
23 | | -import static org.junit.Assert.assertTrue; |
24 | 24 |
|
25 | 25 | /** |
26 | 26 | * @author Gavin King |
27 | 27 | */ |
28 | | -public class DynamicClassTest extends BaseCoreFunctionalTestCase { |
29 | | - |
30 | | - @Override |
31 | | - protected String getBaseForMappings() { |
32 | | - return "org/hibernate/orm/test/"; |
33 | | - } |
34 | | - |
35 | | - @Override |
36 | | - public String[] getMappings() { |
37 | | - return new String[] { "entitymode/map/basic/ProductLine.hbm.xml" }; |
38 | | - } |
39 | | - |
40 | | - @Override |
41 | | - public void configure(Configuration cfg) { |
42 | | - } |
| 28 | +@SessionFactory |
| 29 | +@DomainModel( |
| 30 | + xmlMappings = "org/hibernate/orm/test/entitymode/map/basic/ProductLine.hbm.xml" |
| 31 | +) |
| 32 | +class DynamicClassTest { |
43 | 33 |
|
44 | 34 | @Test |
45 | | - public void testLazyDynamicClass() { |
46 | | - Session s = openSession(); |
47 | | - Transaction t = s.beginTransaction(); |
48 | | - |
49 | | - Map cars = new HashMap(); |
50 | | - cars.put("description", "Cars"); |
51 | | - Map monaro = new HashMap(); |
52 | | - monaro.put("productLine", cars); |
53 | | - monaro.put("name", "monaro"); |
54 | | - monaro.put("description", "Holden Monaro"); |
55 | | - Map hsv = new HashMap(); |
56 | | - hsv.put("productLine", cars); |
57 | | - hsv.put("name", "hsv"); |
58 | | - hsv.put("description", "Holden Commodore HSV"); |
59 | | - List models = new ArrayList(); |
60 | | - cars.put("models", models); |
61 | | - models.add(hsv); |
62 | | - models.add(monaro); |
63 | | - s.persist("ProductLine", cars); |
64 | | - t.commit(); |
65 | | - s.close(); |
66 | | - |
67 | | - s = openSession(); |
68 | | - t = s.beginTransaction(); |
69 | | - |
70 | | - cars = (Map) s.createQuery("from ProductLine pl order by pl.description").uniqueResult(); |
71 | | - models = (List) cars.get("models"); |
72 | | - assertFalse( Hibernate.isInitialized(models) ); |
73 | | - assertEquals( models.size(), 2); |
74 | | - assertTrue( Hibernate.isInitialized(models) ); |
75 | | - |
76 | | - s.clear(); |
77 | | - |
78 | | - List list = s.createQuery("from Model m").list(); |
79 | | - for ( Iterator i=list.iterator(); i.hasNext(); ) { |
80 | | - assertFalse( Hibernate.isInitialized( ( (Map) i.next() ).get("productLine") ) ); |
81 | | - } |
82 | | - Map model = (Map) list.get(0); |
83 | | - assertTrue( ( (List) ( (Map) model.get("productLine") ).get("models") ).contains(model) ); |
84 | | - s.clear(); |
85 | | - |
86 | | - t.commit(); |
87 | | - s.close(); |
88 | | - |
89 | | - s = openSession(); |
90 | | - t = s.beginTransaction(); |
91 | | - cars = (Map) s.createQuery("from ProductLine pl order by pl.description").uniqueResult(); |
92 | | - s.remove(cars); |
93 | | - t.commit(); |
94 | | - s.close(); |
| 35 | + void testLazyDynamicClass(SessionFactoryScope scope) { |
| 36 | + scope.inTransaction( s -> { |
| 37 | + Map<String, Object> cars = new HashMap<>(); |
| 38 | + cars.put( "description", "Cars" ); |
| 39 | + Map<String, Object> monaro = new HashMap<>(); |
| 40 | + monaro.put( "productLine", cars ); |
| 41 | + monaro.put( "name", "monaro" ); |
| 42 | + monaro.put( "description", "Holden Monaro" ); |
| 43 | + Map<String, Object> hsv = new HashMap<>(); |
| 44 | + hsv.put( "productLine", cars ); |
| 45 | + hsv.put( "name", "hsv" ); |
| 46 | + hsv.put( "description", "Holden Commodore HSV" ); |
| 47 | + List<Map<String, Object>> models = new ArrayList<>(); |
| 48 | + cars.put( "models", models ); |
| 49 | + models.add( hsv ); |
| 50 | + models.add( monaro ); |
| 51 | + s.persist( "ProductLine", cars ); |
| 52 | + } ); |
| 53 | + |
| 54 | + scope.inTransaction( s -> { |
| 55 | + Map<String, Object> cars = (Map<String, Object>) s.createQuery( |
| 56 | + "from ProductLine pl order by pl.description" ).uniqueResult(); |
| 57 | + List<Map<String, Object>> models = (List<Map<String, Object>>) cars.get( "models" ); |
| 58 | + assertFalse( Hibernate.isInitialized( models ) ); |
| 59 | + assertEquals( 2, models.size() ); |
| 60 | + assertTrue( Hibernate.isInitialized( models ) ); |
| 61 | + |
| 62 | + s.clear(); |
| 63 | + |
| 64 | + List<?> list = s.createQuery( "from Model m" ).list(); |
| 65 | + for ( Iterator<?> i = list.iterator(); i.hasNext(); ) { |
| 66 | + assertFalse( Hibernate.isInitialized( ((Map<String, Object>) i.next()).get( "productLine" ) ) ); |
| 67 | + } |
| 68 | + Map<String, Object> model = (Map<String, Object>) list.get( 0 ); |
| 69 | + assertTrue( ((List<Map<String, Object>>) ((Map<String, Object>) model.get( "productLine" )).get( |
| 70 | + "models" )).contains( model ) ); |
| 71 | + s.clear(); |
| 72 | + |
| 73 | + } ); |
| 74 | + |
| 75 | + scope.inTransaction( s -> { |
| 76 | + Map<String, Object> cars = (Map<String, Object>) s.createQuery( |
| 77 | + "from ProductLine pl order by pl.description" ).uniqueResult(); |
| 78 | + s.remove( cars ); |
| 79 | + } ); |
95 | 80 | } |
96 | 81 |
|
| 82 | + @Test |
| 83 | + void multiload(SessionFactoryScope scope) { |
| 84 | + final Object id = scope.fromTransaction( s -> { |
| 85 | + Map<String, Object> cars = new HashMap<>(); |
| 86 | + cars.put( "description", "Cars" ); |
| 87 | + Map<String, Object> monaro = new HashMap<>(); |
| 88 | + monaro.put( "productLine", cars ); |
| 89 | + monaro.put( "name", "monaro" ); |
| 90 | + monaro.put( "description", "Holden Monaro" ); |
| 91 | + Map<String, Object> hsv = new HashMap<>(); |
| 92 | + hsv.put( "productLine", cars ); |
| 93 | + hsv.put( "name", "hsv" ); |
| 94 | + hsv.put( "description", "Holden Commodore HSV" ); |
| 95 | + List<Map<String, Object>> models = new ArrayList<>(); |
| 96 | + cars.put( "models", models ); |
| 97 | + models.add( hsv ); |
| 98 | + models.add( monaro ); |
| 99 | + s.persist( "ProductLine", cars ); |
| 100 | + |
| 101 | + return cars.get( "id" ); |
| 102 | + } ); |
| 103 | + |
| 104 | + scope.inTransaction( s -> { |
| 105 | + var rootGraph = s.getSessionFactory().createGraphForDynamicEntity( "ProductLine" ); |
| 106 | + |
| 107 | + List<Map<String, ?>> found = s.findMultiple( rootGraph, List.of( id ) ); |
| 108 | + |
| 109 | + assertThat( found ).hasSize( 1 ); |
| 110 | + } ); |
97 | 111 |
|
| 112 | + } |
98 | 113 | } |
0 commit comments