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

(cherry picked from commit 7232b7ccd5)
This commit is contained in:
Janario Oliveira 2016-03-09 18:46:51 -03:00 committed by Gail Badner
parent bc917f1176
commit 337ee18b19
2 changed files with 17 additions and 7 deletions

View File

@ -736,13 +736,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();