|
12 | 12 |
|
13 | 13 | import java.util.ArrayList; |
14 | 14 | import java.util.HashMap; |
15 | | -import java.util.Iterator; |
16 | 15 | import java.util.List; |
17 | 16 | import java.util.Map; |
18 | 17 |
|
19 | 18 | 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; |
23 | | - |
24 | 19 |
|
25 | 20 | /** |
26 | 21 | * @author Gavin King |
27 | 22 | */ |
28 | | -@SessionFactory |
29 | 23 | @DomainModel( |
30 | 24 | xmlMappings = "org/hibernate/orm/test/entitymode/map/basic/ProductLine.hbm.xml" |
31 | 25 | ) |
32 | | -class DynamicClassTest { |
| 26 | +@SessionFactory |
| 27 | +public class DynamicClassTest { |
33 | 28 |
|
34 | 29 | @Test |
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 | | - } ); |
| 30 | + public void testLazyDynamicClass(SessionFactoryScope scope) { |
| 31 | + |
| 32 | + scope.inTransaction( |
| 33 | + session -> { |
| 34 | + Map<String, Object> cars = new HashMap<>(); |
| 35 | + cars.put( "description", "Cars" ); |
| 36 | + |
| 37 | + Map<String, Object> monaro = new HashMap<>(); |
| 38 | + monaro.put( "productLine", cars ); |
| 39 | + monaro.put( "name", "monaro" ); |
| 40 | + monaro.put( "description", "Holden Monaro" ); |
| 41 | + |
| 42 | + Map<String, Object> hsv = new HashMap<>(); |
| 43 | + hsv.put( "productLine", cars ); |
| 44 | + hsv.put( "name", "hsv" ); |
| 45 | + hsv.put( "description", "Holden Commodore HSV" ); |
| 46 | + |
| 47 | + List<Map<String, Object>> models = new ArrayList<>(); |
| 48 | + cars.put( "models", models ); |
| 49 | + models.add( hsv ); |
| 50 | + models.add( monaro ); |
| 51 | + |
| 52 | + session.persist( "ProductLine", cars ); |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + scope.inTransaction( |
| 57 | + session -> { |
| 58 | + Map<String, Object> cars = (Map<String, Object>) session |
| 59 | + .createQuery( "from ProductLine pl order by pl.description" ) |
| 60 | + .uniqueResult(); |
| 61 | + List<Map<String, Object>> models = (List<Map<String, Object>>) cars.get( "models" ); |
| 62 | + assertThat( Hibernate.isInitialized( models ) ).isFalse(); |
| 63 | + assertThat( models ).hasSize( 2 ); |
| 64 | + assertThat( Hibernate.isInitialized( models ) ).isTrue(); |
| 65 | + |
| 66 | + session.clear(); |
| 67 | + |
| 68 | + List<Map<String, Map<String, Object>>> list = session.createQuery( "from Model m" ).list(); |
| 69 | + for ( Map<String, Map<String, Object>> stringObjectMap : list ) { |
| 70 | + assertThat( Hibernate.isInitialized( stringObjectMap.get( "productLine" ) ) ).isFalse(); |
| 71 | + } |
| 72 | + Map<String, Map<String, Object>> model = list.get( 0 ); |
| 73 | + assertThat( ((((List<Map<String, Object>>) (model.get( "productLine" )).get( "models" )) ).contains( model ); |
| 74 | + session.clear(); |
| 75 | + } |
| 76 | + ); |
| 77 | + |
| 78 | + |
| 79 | + scope.inTransaction( |
| 80 | + session -> { |
| 81 | + session.remove( |
| 82 | + session.createQuery( "from ProductLine pl order by pl.description" ).uniqueResult() ); |
| 83 | + } |
| 84 | + ); |
80 | 85 | } |
81 | 86 |
|
82 | 87 | @Test |
|
0 commit comments