Skip to content

Commit 86c8e97

Browse files
marko-bekhtambellade
authored andcommitted
HHH-19851 Handle the calls to Session#findMultiple(EntityGraph...) for dynamic entities
1 parent 04ec9c6 commit 86c8e97

File tree

2 files changed

+96
-76
lines changed

2 files changed

+96
-76
lines changed

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,12 @@ public <E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption...
989989
@Override
990990
public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options) {
991991
final var rootGraph = (RootGraph<E>) entityGraph;
992-
final var loadAccess = byMultipleIds( rootGraph.getGraphedType().getJavaType() );
992+
final var type = rootGraph.getGraphedType();
993+
final MultiIdentifierLoadAccess<E> loadAccess =
994+
switch ( type.getRepresentationMode() ) {
995+
case MAP -> byMultipleIds( type.getTypeName() );
996+
case POJO -> byMultipleIds( type.getJavaType() );
997+
};
993998
loadAccess.withLoadGraph( rootGraph );
994999
setMultiIdentifierLoadAccessOptions( options, loadAccess );
9951000
return loadAccess.multiLoad( ids );

hibernate-core/src/test/java/org/hibernate/orm/test/entitymode/map/basic/DynamicClassTest.java

Lines changed: 90 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,110 @@
44
*/
55
package org.hibernate.orm.test.entitymode.map.basic;
66

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+
713
import java.util.ArrayList;
814
import java.util.HashMap;
915
import java.util.Iterator;
1016
import java.util.List;
1117
import java.util.Map;
1218

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;
2023

21-
import static org.junit.Assert.assertEquals;
22-
import static org.junit.Assert.assertFalse;
23-
import static org.junit.Assert.assertTrue;
2424

2525
/**
2626
* @author Gavin King
2727
*/
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 {
4333

4434
@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+
} );
9580
}
9681

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+
} );
97111

112+
}
98113
}

0 commit comments

Comments
 (0)