|
4 | 4 | */
|
5 | 5 | package org.hibernate.orm.test.batchfetch;
|
6 | 6 |
|
7 |
| -import java.util.ArrayList; |
8 |
| -import java.util.Iterator; |
9 |
| -import java.util.List; |
10 |
| - |
11 | 7 | import org.hibernate.Hibernate;
|
12 |
| -import org.hibernate.cfg.AvailableSettings; |
13 |
| - |
14 | 8 | import org.hibernate.testing.orm.junit.DomainModel;
|
15 | 9 | import org.hibernate.testing.orm.junit.ServiceRegistry;
|
16 | 10 | import org.hibernate.testing.orm.junit.SessionFactory;
|
|
19 | 13 | import org.junit.jupiter.api.AfterEach;
|
20 | 14 | import org.junit.jupiter.api.Test;
|
21 | 15 |
|
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.hibernate.cfg.CacheSettings.USE_SECOND_LEVEL_CACHE; |
22 | 22 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
24 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
27 | 27 | /**
|
28 | 28 | * @author Gavin King
|
29 | 29 | */
|
| 30 | +@SuppressWarnings("JUnitMalformedDeclaration") |
30 | 31 | @DomainModel(
|
31 |
| - xmlMappings = "org/hibernate/orm/test/batchfetch/ProductLine.hbm.xml", |
| 32 | + xmlMappings = "org/hibernate/orm/test/batchfetch/ProductLine.xml", |
32 | 33 | annotatedClasses = BatchLoadableEntity.class
|
33 | 34 | )
|
34 |
| -@SessionFactory( |
35 |
| - generateStatistics = true |
36 |
| -) |
37 |
| -@ServiceRegistry( |
38 |
| - settings = { |
39 |
| - @Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "false") |
40 |
| - } |
41 |
| -) |
| 35 | +@SessionFactory(generateStatistics = true) |
| 36 | +@ServiceRegistry(settings = @Setting(name = USE_SECOND_LEVEL_CACHE, value = "false")) |
42 | 37 | public class BatchFetchTest {
|
43 | 38 |
|
44 | 39 | @SuppressWarnings("unchecked")
|
45 | 40 | @Test
|
46 | 41 | public void testBatchFetch(SessionFactoryScope scope) {
|
47 |
| - ProductLine ossProductLine = new ProductLine(); |
48 |
| - Model hibernateModel = new Model( ossProductLine ); |
49 |
| - scope.inTransaction( |
50 |
| - session -> { |
51 |
| - ProductLine cars = new ProductLine(); |
52 |
| - cars.setDescription( "Cars" ); |
53 |
| - Model monaro = new Model( cars ); |
54 |
| - monaro.setName( "monaro" ); |
55 |
| - monaro.setDescription( "Holden Monaro" ); |
56 |
| - Model hsv = new Model( cars ); |
57 |
| - hsv.setName( "hsv" ); |
58 |
| - hsv.setDescription( "Holden Commodore HSV" ); |
59 |
| - session.persist( cars ); |
60 |
| - |
61 |
| - ossProductLine.setDescription( "OSS" ); |
62 |
| - Model jboss = new Model( ossProductLine ); |
63 |
| - jboss.setName( "JBoss" ); |
64 |
| - jboss.setDescription( "JBoss Application Server" ); |
65 |
| - |
66 |
| - hibernateModel.setName( "Hibernate" ); |
67 |
| - hibernateModel.setDescription( "Hibernate" ); |
68 |
| - Model cache = new Model( ossProductLine ); |
69 |
| - cache.setName( "JBossCache" ); |
70 |
| - cache.setDescription( "JBoss TreeCache" ); |
71 |
| - session.persist( ossProductLine ); |
72 |
| - } |
73 |
| - ); |
74 |
| - |
75 |
| - scope.getSessionFactory().getCache().evictEntityData( Model.class ); |
76 |
| - scope.getSessionFactory().getCache().evictEntityData( ProductLine.class ); |
77 |
| - |
78 |
| - scope.inTransaction( |
79 |
| - session -> { |
80 |
| - List<ProductLine> list = session.createQuery( "from ProductLine pl order by pl.description" ) |
81 |
| - .list(); |
82 |
| - ProductLine cars = list.get( 0 ); |
83 |
| - ProductLine oss = list.get( 1 ); |
84 |
| - assertFalse( Hibernate.isInitialized( cars.getModels() ) ); |
85 |
| - assertFalse( Hibernate.isInitialized( oss.getModels() ) ); |
86 |
| - assertEquals( 2, cars.getModels().size() ); //fetch both collections |
87 |
| - assertTrue( Hibernate.isInitialized( cars.getModels() ) ); |
88 |
| - assertTrue( Hibernate.isInitialized( oss.getModels() ) ); |
89 |
| - |
90 |
| - session.clear(); |
91 |
| - |
92 |
| - List<Model> models = session.createQuery( "from Model m" ).list(); |
93 |
| - Model hibernate = session.get( Model.class, hibernateModel.getId() ); |
94 |
| - hibernate.getProductLine().getId(); |
95 |
| - for ( Model aList : models ) { |
96 |
| - assertFalse( Hibernate.isInitialized( aList.getProductLine() ) ); |
97 |
| - } |
98 |
| - assertEquals( hibernate.getProductLine().getDescription(), "OSS" ); //fetch both productlines |
99 |
| - |
100 |
| - session.clear(); |
101 |
| - |
102 |
| - Iterator<Model> iter = session.createQuery( "from Model" ).list().iterator(); |
103 |
| - models = new ArrayList(); |
104 |
| - while ( iter.hasNext() ) { |
105 |
| - models.add( iter.next() ); |
106 |
| - } |
107 |
| - Model m = models.get( 0 ); |
108 |
| - m.getDescription(); //fetch a batch of 4 |
109 |
| - |
110 |
| - session.clear(); |
111 |
| - |
112 |
| - list = session.createQuery( "from ProductLine" ).list(); |
113 |
| - ProductLine pl = list.get( 0 ); |
114 |
| - ProductLine pl2 = list.get( 1 ); |
115 |
| - session.evict( pl2 ); |
116 |
| - pl.getModels().size(); //fetch just one collection! (how can we write an assertion for that??) |
117 |
| - } |
118 |
| - ); |
119 |
| - |
120 |
| - scope.inTransaction( |
121 |
| - session -> { |
122 |
| - List<ProductLine> list = session.createQuery( "from ProductLine pl order by pl.description" ) |
123 |
| - .list(); |
124 |
| - ProductLine cars = list.get( 0 ); |
125 |
| - ProductLine oss = list.get( 1 ); |
126 |
| - assertEquals( cars.getModels().size(), 2 ); |
127 |
| - assertEquals( oss.getModels().size(), 3 ); |
128 |
| - session.remove( cars ); |
129 |
| - session.remove( oss ); |
130 |
| - } |
131 |
| - ); |
| 42 | + ProductLine ossProductLine = new ProductLine( "OSS" ); |
| 43 | + Model hibernateModel = new Model( "Hibernate", "Hibernate", ossProductLine ); |
| 44 | + scope.inTransaction( (session) -> { |
| 45 | + ProductLine cars = new ProductLine( "Cars" ); |
| 46 | + new Model( "monaro", "Holden Monaro", cars ); |
| 47 | + new Model( "hsv", "Holden Commodore HSV", cars ); |
| 48 | + session.persist( cars ); |
| 49 | + |
| 50 | + ossProductLine.setDescription( "OSS" ); |
| 51 | + new Model( "JBoss", "JBoss Application Server", ossProductLine ); |
| 52 | + new Model( "JBossCache", "JBoss TreeCache", ossProductLine ); |
| 53 | + session.persist( ossProductLine ); |
| 54 | + } ); |
| 55 | + |
| 56 | + scope.inTransaction( (session) -> { |
| 57 | + List<ProductLine> list = session.createQuery( "from ProductLine pl order by pl.description" ).list(); |
| 58 | + ProductLine cars = list.get( 0 ); |
| 59 | + ProductLine oss = list.get( 1 ); |
| 60 | + assertFalse( Hibernate.isInitialized( cars.getModels() ) ); |
| 61 | + assertFalse( Hibernate.isInitialized( oss.getModels() ) ); |
| 62 | + assertEquals( 2, cars.getModels().size() ); //fetch both collections |
| 63 | + assertTrue( Hibernate.isInitialized( cars.getModels() ) ); |
| 64 | + assertTrue( Hibernate.isInitialized( oss.getModels() ) ); |
| 65 | + } ); |
| 66 | + |
| 67 | + scope.inTransaction( (session) -> { |
| 68 | + List<Model> models = session.createQuery( "from Model m" ).list(); |
| 69 | + Model hibernate = session.find( Model.class, hibernateModel.getId() ); |
| 70 | + hibernate.getProductLine().getId(); |
| 71 | + for ( Model aList : models ) { |
| 72 | + assertFalse( Hibernate.isInitialized( aList.getProductLine() ) ); |
| 73 | + } |
| 74 | + //fetch both product lines |
| 75 | + assertThat( hibernate.getProductLine().getDescription() ).isEqualTo( "OSS" ); |
| 76 | + } ); |
| 77 | + |
| 78 | + scope.inTransaction( (session) -> { |
| 79 | + Iterator<Model> iter = session.createQuery( "from Model" ).list().iterator(); |
| 80 | + ArrayList<Model> models = new ArrayList<>(); |
| 81 | + while ( iter.hasNext() ) { |
| 82 | + models.add( iter.next() ); |
| 83 | + } |
| 84 | + Model m = models.get( 0 ); |
| 85 | + m.getDescription(); //fetch a batch of 4 |
| 86 | + |
| 87 | + session.clear(); |
| 88 | + |
| 89 | + List<ProductLine> list = session.createQuery( "from ProductLine" ).list(); |
| 90 | + ProductLine pl = list.get( 0 ); |
| 91 | + ProductLine pl2 = list.get( 1 ); |
| 92 | + session.evict( pl2 ); |
| 93 | + pl.getModels().size(); //fetch just one collection! (how can we write an assertion for that??) |
| 94 | + } ); |
| 95 | + |
| 96 | + scope.inTransaction( (session) -> { |
| 97 | + List<ProductLine> list = session.createQuery( "from ProductLine pl order by pl.description" ).list(); |
| 98 | + ProductLine cars = list.get( 0 ); |
| 99 | + ProductLine oss = list.get( 1 ); |
| 100 | + assertThat( cars.getModels().size() ).isEqualTo( 2 ); |
| 101 | + assertThat( oss.getModels().size() ).isEqualTo( 3 ); |
| 102 | + } ); |
132 | 103 | }
|
133 | 104 |
|
134 | 105 | @Test
|
135 |
| - @SuppressWarnings("unchecked") |
136 | 106 | public void testBatchFetch2(SessionFactoryScope scope) {
|
137 | 107 | int size = 32 + 14;
|
138 |
| - scope.inTransaction( |
139 |
| - session -> { |
140 |
| - for ( int i = 0; i < size; i++ ) { |
141 |
| - session.persist( new BatchLoadableEntity( i ) ); |
142 |
| - } |
143 |
| - } |
144 |
| - ); |
145 |
| - |
146 |
| - scope.inTransaction( |
147 |
| - session -> { |
148 |
| - // load them all as proxies |
149 |
| - for ( int i = 0; i < size; i++ ) { |
150 |
| - BatchLoadableEntity entity = session.getReference( BatchLoadableEntity.class, i ); |
151 |
| - assertFalse( Hibernate.isInitialized( entity ) ); |
152 |
| - } |
153 |
| - scope.getSessionFactory().getStatistics().clear(); |
154 |
| - // now start initializing them... |
155 |
| - for ( int i = 0; i < size; i++ ) { |
156 |
| - BatchLoadableEntity entity = session.getReference( BatchLoadableEntity.class, i ); |
157 |
| - Hibernate.initialize( entity ); |
158 |
| - assertTrue( Hibernate.isInitialized( entity ) ); |
159 |
| - } |
160 |
| - // so at this point, all entities are initialized. see how many fetches were performed. |
161 |
| - final int expectedFetchCount; |
162 |
| -// if ( sessionFactory().getSettings().getBatchFetchStyle() == BatchFetchStyle.LEGACY ) { |
163 |
| -// expectedFetchCount = 3; // (32 + 10 + 4) |
164 |
| -// } |
165 |
| -// else if ( sessionFactory().getSettings().getBatchFetchStyle() == BatchFetchStyle.DYNAMIC ) { |
166 |
| -// expectedFetchCount = 2; // (32 + 14) : because we limited batch-size to 32 |
167 |
| -// } |
168 |
| -// else { |
169 |
| - // PADDED |
170 |
| - expectedFetchCount = 2; // (32 + 16*) with the 16 being padded |
171 |
| -// } |
172 |
| - assertEquals( |
173 |
| - expectedFetchCount, |
174 |
| - scope.getSessionFactory().getStatistics() |
175 |
| - .getEntityStatistics( BatchLoadableEntity.class.getName() ) |
176 |
| - .getFetchCount() |
177 |
| - ); |
178 |
| - } |
179 |
| - ); |
| 108 | + scope.inTransaction( (session) -> { |
| 109 | + for ( int i = 0; i < size; i++ ) { |
| 110 | + session.persist( new BatchLoadableEntity( i ) ); |
| 111 | + } |
| 112 | + } ); |
| 113 | + |
| 114 | + scope.inTransaction( (session) -> { |
| 115 | + // load them all as proxies |
| 116 | + for ( int i = 0; i < size; i++ ) { |
| 117 | + BatchLoadableEntity entity = session.getReference( BatchLoadableEntity.class, i ); |
| 118 | + assertFalse( Hibernate.isInitialized( entity ) ); |
| 119 | + } |
| 120 | + scope.getSessionFactory().getStatistics().clear(); |
| 121 | + // now start initializing them... |
| 122 | + for ( int i = 0; i < size; i++ ) { |
| 123 | + BatchLoadableEntity entity = session.getReference( BatchLoadableEntity.class, i ); |
| 124 | + Hibernate.initialize( entity ); |
| 125 | + assertTrue( Hibernate.isInitialized( entity ) ); |
| 126 | + } |
| 127 | + // so at this point, all entities are initialized. see how many fetches were performed. |
| 128 | + final int expectedFetchCount; |
| 129 | + expectedFetchCount = 2; // (32 + 16*) with the 16 being padded |
| 130 | + |
| 131 | + assertEquals( |
| 132 | + expectedFetchCount, |
| 133 | + scope.getSessionFactory().getStatistics() |
| 134 | + .getEntityStatistics( BatchLoadableEntity.class.getName() ) |
| 135 | + .getFetchCount() |
| 136 | + ); |
| 137 | + } ); |
180 | 138 | }
|
181 | 139 |
|
182 | 140 | @AfterEach
|
183 | 141 | public void tearDown(SessionFactoryScope scope) {
|
184 |
| - scope.inTransaction( |
185 |
| - session -> { |
186 |
| - session.createQuery( "delete BatchLoadableEntity" ).executeUpdate(); |
187 |
| - session.createQuery( "delete Model" ).executeUpdate(); |
188 |
| - session.createQuery( "delete ProductLine" ).executeUpdate(); |
189 |
| - } |
190 |
| - ); |
| 142 | + scope.dropData(); |
191 | 143 | }
|
192 | 144 | }
|
0 commit comments