HHH-10608 - Avoid exception from Hibernate.initialize when lazy_no_transaction is enabled

This commit is contained in:
Janario Oliveira 2016-03-09 18:46:51 -03:00 committed by Vlad Mihalcea
parent bdb458a609
commit 7232b7ccd5
2 changed files with 17 additions and 7 deletions

View File

@ -728,13 +728,7 @@ public abstract class AbstractPersistentCollection implements Serializable, Pers
if ( initializing ) {
throw new AssertionFailure( "force initialize loading collection" );
}
if ( session == null ) {
throw new HibernateException( "collection is not associated with any session" );
}
if ( !session.isConnected() ) {
throw new HibernateException( "disconnected session" );
}
session.initializeCollection( this, false );
initialize( false );
}
}

View File

@ -21,6 +21,7 @@ import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@ -53,6 +54,21 @@ public class CacheLazyLoadNoTransTest extends BaseNonConfigCoreFunctionalTestCas
settings.put( Environment.CACHE_PROVIDER_CONFIG, "true" );
}
@Test
public void hibernateInitialize() {
Customer customer = new Customer();
Item item1 = new Item( customer );
Item item2 = new Item( customer );
customer.boughtItems.add( item1 );
customer.boughtItems.add( item2 );
persist( customer );
customer = find( Customer.class, customer.id );
assertFalse( Hibernate.isInitialized( customer.boughtItems ) );
Hibernate.initialize( customer.boughtItems );
assertTrue( Hibernate.isInitialized( customer.boughtItems ) );
}
@Test
public void testOneToMany() {
Customer customer = new Customer();