Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -990,8 +990,12 @@ public <E> List<E> findMultiple(Class<E> entityType, List<?> ids, FindOption...
@Override
public <E> List<E> findMultiple(EntityGraph<E> entityGraph, List<?> ids, FindOption... options) {
final RootGraph<E> rootGraph = (RootGraph<E>) entityGraph;
final ManagedDomainType<E> type = rootGraph.getGraphedType();
final MultiIdentifierLoadAccess<E> loadAccess =
byMultipleIds( rootGraph.getGraphedType().getJavaType() );
switch ( type.getRepresentationMode() ) {
case MAP -> byMultipleIds( type.getTypeName() );
case POJO -> byMultipleIds( type.getJavaType() );
};
loadAccess.withLoadGraph( rootGraph );
setMultiIdentifierLoadAccessOptions( options, loadAccess );
return loadAccess.multiLoad( ids );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,95 +4,110 @@
*/
package org.hibernate.orm.test.entitymode.map.basic;

import org.hibernate.Hibernate;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* @author Gavin King
*/
public class DynamicClassTest extends BaseCoreFunctionalTestCase {

@Override
protected String getBaseForMappings() {
return "org/hibernate/orm/test/";
}

@Override
public String[] getMappings() {
return new String[] { "entitymode/map/basic/ProductLine.hbm.xml" };
}

@Override
public void configure(Configuration cfg) {
}
@SessionFactory
@DomainModel(
xmlMappings = "org/hibernate/orm/test/entitymode/map/basic/ProductLine.hbm.xml"
)
class DynamicClassTest {

@Test
public void testLazyDynamicClass() {
Session s = openSession();
Transaction t = s.beginTransaction();

Map cars = new HashMap();
cars.put("description", "Cars");
Map monaro = new HashMap();
monaro.put("productLine", cars);
monaro.put("name", "monaro");
monaro.put("description", "Holden Monaro");
Map hsv = new HashMap();
hsv.put("productLine", cars);
hsv.put("name", "hsv");
hsv.put("description", "Holden Commodore HSV");
List models = new ArrayList();
cars.put("models", models);
models.add(hsv);
models.add(monaro);
s.persist("ProductLine", cars);
t.commit();
s.close();

s = openSession();
t = s.beginTransaction();

cars = (Map) s.createQuery("from ProductLine pl order by pl.description").uniqueResult();
models = (List) cars.get("models");
assertFalse( Hibernate.isInitialized(models) );
assertEquals( models.size(), 2);
assertTrue( Hibernate.isInitialized(models) );

s.clear();

List list = s.createQuery("from Model m").list();
for ( Iterator i=list.iterator(); i.hasNext(); ) {
assertFalse( Hibernate.isInitialized( ( (Map) i.next() ).get("productLine") ) );
}
Map model = (Map) list.get(0);
assertTrue( ( (List) ( (Map) model.get("productLine") ).get("models") ).contains(model) );
s.clear();

t.commit();
s.close();

s = openSession();
t = s.beginTransaction();
cars = (Map) s.createQuery("from ProductLine pl order by pl.description").uniqueResult();
s.remove(cars);
t.commit();
s.close();
void testLazyDynamicClass(SessionFactoryScope scope) {
scope.inTransaction( s -> {
Map<String, Object> cars = new HashMap<>();
cars.put( "description", "Cars" );
Map<String, Object> monaro = new HashMap<>();
monaro.put( "productLine", cars );
monaro.put( "name", "monaro" );
monaro.put( "description", "Holden Monaro" );
Map<String, Object> hsv = new HashMap<>();
hsv.put( "productLine", cars );
hsv.put( "name", "hsv" );
hsv.put( "description", "Holden Commodore HSV" );
List<Map<String, Object>> models = new ArrayList<>();
cars.put( "models", models );
models.add( hsv );
models.add( monaro );
s.persist( "ProductLine", cars );
} );

scope.inTransaction( s -> {
Map<String, Object> cars = (Map<String, Object>) s.createQuery(
"from ProductLine pl order by pl.description" ).uniqueResult();
List<Map<String, Object>> models = (List<Map<String, Object>>) cars.get( "models" );
assertFalse( Hibernate.isInitialized( models ) );
assertEquals( 2, models.size() );
assertTrue( Hibernate.isInitialized( models ) );

s.clear();

List<?> list = s.createQuery( "from Model m" ).list();
for ( Iterator<?> i = list.iterator(); i.hasNext(); ) {
assertFalse( Hibernate.isInitialized( ((Map<String, Object>) i.next()).get( "productLine" ) ) );
}
Map<String, Object> model = (Map<String, Object>) list.get( 0 );
assertTrue( ((List<Map<String, Object>>) ((Map<String, Object>) model.get( "productLine" )).get(
"models" )).contains( model ) );
s.clear();

} );

scope.inTransaction( s -> {
Map<String, Object> cars = (Map<String, Object>) s.createQuery(
"from ProductLine pl order by pl.description" ).uniqueResult();
s.remove( cars );
} );
}

@Test
void multiload(SessionFactoryScope scope) {
final Object id = scope.fromTransaction( s -> {
Map<String, Object> cars = new HashMap<>();
cars.put( "description", "Cars" );
Map<String, Object> monaro = new HashMap<>();
monaro.put( "productLine", cars );
monaro.put( "name", "monaro" );
monaro.put( "description", "Holden Monaro" );
Map<String, Object> hsv = new HashMap<>();
hsv.put( "productLine", cars );
hsv.put( "name", "hsv" );
hsv.put( "description", "Holden Commodore HSV" );
List<Map<String, Object>> models = new ArrayList<>();
cars.put( "models", models );
models.add( hsv );
models.add( monaro );
s.persist( "ProductLine", cars );

return cars.get( "id" );
} );

scope.inTransaction( s -> {
var rootGraph = s.getSessionFactory().createGraphForDynamicEntity( "ProductLine" );

List<Map<String, ?>> found = s.findMultiple( rootGraph, List.of( id ) );

assertThat( found ).hasSize( 1 );
} );

}
}
Loading