Re-enabled additional tests

This commit is contained in:
Andrea Boriero 2020-06-24 15:00:00 +01:00
parent 3ad68d3f88
commit b84d41a66c
14 changed files with 1434 additions and 1354 deletions

View File

@ -21,21 +21,16 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
import org.junit.Test;
package org.hibernate.orm.test.collection.dereferenced;
import org.hibernate.*;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import java.lang.InstantiationException;
import java.util.HashSet;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.orm.junit.SessionFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@ -46,7 +41,7 @@ import static org.junit.Assert.assertTrue;
/**
* @author Gail Badner
*/
public abstract class AbstractDereferencedCollectionTest extends BaseCoreFunctionalTestCase {
public abstract class AbstractDereferencedCollectionTest {
protected EntityEntry getEntityEntry(Session s, Object entity) {
return ( (SessionImplementor) s ).getPersistenceContextInternal().getEntry( entity );

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
package org.hibernate.orm.test.collection.dereferenced;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -0,0 +1,360 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.orm.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Gail Badner
*/
@DomainModel(
annotatedClasses = {
UnversionedCascadeOne.class,
Many.class
}
)
@SessionFactory
public class UnversionedCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection(SessionFactoryScope scope) {
UnversionedCascadeOne unversionedCascadeOne = new UnversionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedCascadeOne.getManies() );
session.save( unversionedCascadeOne );
assertNull( unversionedCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = UnversionedCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
UnversionedCascadeOne one = (UnversionedCascadeOne) session.merge( unversionedCascadeOne );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( session, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection(SessionFactoryScope scope) {
UnversionedCascadeOne unversionedCascadeOne = new UnversionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedCascadeOne.getManies() );
session.save( unversionedCascadeOne );
assertNull( unversionedCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = UnversionedCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
UnversionedCascadeOne one = session.get(
UnversionedCascadeOne.class,
unversionedCascadeOne.getId()
);
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManies.getLoadedPersister()
);
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection(SessionFactoryScope scope) {
UnversionedCascadeOne unversionedCascadeOne = new UnversionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedCascadeOne.getManies() );
session.save( unversionedCascadeOne );
assertNull( unversionedCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = UnversionedCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
UnversionedCascadeOne one = session.get(
UnversionedCascadeOne.class,
unversionedCascadeOne.getId()
);
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<>() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( session, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry(
session,
(PersistentCollection) one.getManies()
);
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesAfterReplace.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
public void testSaveOrUpdateNullCollection(SessionFactoryScope scope) {
UnversionedCascadeOne one = new UnversionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( one.getManies() );
session.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( session, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
scope.inTransaction(
session -> {
session.saveOrUpdate( one );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
}
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
package org.hibernate.orm.test.collection.dereferenced;
import java.util.Set;
import javax.persistence.CascadeType;
@ -30,7 +30,6 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Version;
/**
* @author Gail Badner

View File

@ -0,0 +1,355 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.orm.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Gail Badner
*/
@DomainModel(
annotatedClasses = {
UnversionedNoCascadeOne.class,
Many.class
})
@SessionFactory
public class UnversionedNoCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection(SessionFactoryScope scope) {
UnversionedNoCascadeOne unversionedNoCascadeOne = new UnversionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedNoCascadeOne.getManies() );
session.save( unversionedNoCascadeOne );
assertNull( unversionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
UnversionedNoCascadeOne one = (UnversionedNoCascadeOne) session.merge( unversionedNoCascadeOne );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( session, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection(SessionFactoryScope scope) {
UnversionedNoCascadeOne unversionedNoCascadeOne = new UnversionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedNoCascadeOne.getManies() );
session.save( unversionedNoCascadeOne );
assertNull( unversionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
UnversionedNoCascadeOne one = session.get(
UnversionedNoCascadeOne.class,
unversionedNoCascadeOne.getId()
);
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManies.getLoadedPersister()
);
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection(SessionFactoryScope scope) {
UnversionedNoCascadeOne unversionedNoCascadeOne = new UnversionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedNoCascadeOne.getManies() );
session.save( unversionedNoCascadeOne );
assertNull( unversionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
UnversionedNoCascadeOne one = session.get(
UnversionedNoCascadeOne.class,
unversionedNoCascadeOne.getId()
);
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<>() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( session, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry(
session,
(PersistentCollection) one.getManies()
);
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesAfterReplace.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
public void testSaveOrUpdateNullCollection(SessionFactoryScope scope) {
UnversionedNoCascadeOne unversionedNoCascadeOne = new UnversionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( unversionedNoCascadeOne.getManies() );
session.save( unversionedNoCascadeOne );
assertNull( unversionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, unversionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( unversionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
scope.inTransaction(
session -> {
session.saveOrUpdate( unversionedNoCascadeOne );
// Ensure one.getManies() is still null.
assertNull( unversionedNoCascadeOne.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
EntityEntry eeOne = getEntityEntry( session, unversionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
// Ensure one.getManies() is still null.
assertNull( unversionedNoCascadeOne.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, unversionedNoCascadeOne ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
}
}

View File

@ -21,10 +21,9 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
package org.hibernate.orm.test.collection.dereferenced;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

View File

@ -0,0 +1,352 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.orm.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Gail Badner
*/
@DomainModel(
annotatedClasses = {
VersionedCascadeOne.class,
Many.class
}
)
@SessionFactory
public class VersionedCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection(SessionFactoryScope scope) {
VersionedCascadeOne versionedCascadeOne = new VersionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedCascadeOne.getManies() );
session.save( versionedCascadeOne );
assertNull( versionedCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = VersionedCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
VersionedCascadeOne one = (VersionedCascadeOne) session.merge( versionedCascadeOne );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( session, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection(SessionFactoryScope scope) {
VersionedCascadeOne versionedCascadeOne = new VersionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedCascadeOne.getManies() );
session.save( versionedCascadeOne );
assertNull( versionedCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = VersionedCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
VersionedCascadeOne one = session.get( VersionedCascadeOne.class, versionedCascadeOne.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManies.getLoadedPersister()
);
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection(SessionFactoryScope scope) {
VersionedCascadeOne versionedCascadeOne = new VersionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedCascadeOne.getManies() );
session.save( versionedCascadeOne );
assertNull( versionedCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = VersionedCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
VersionedCascadeOne one = session.get( VersionedCascadeOne.class, versionedCascadeOne.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<>() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( session, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry(
session,
(PersistentCollection) one.getManies()
);
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesAfterReplace.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
public void testSaveOrUpdateNullCollection(SessionFactoryScope scope) {
VersionedCascadeOne one = new VersionedCascadeOne();
scope.inTransaction(
session -> {
assertNull( one.getManies() );
session.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( session, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
scope.inTransaction(
session -> {
session.saveOrUpdate( one );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
}
}

View File

@ -21,7 +21,7 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
package org.hibernate.orm.test.collection.dereferenced;
import java.util.Set;
import javax.persistence.CascadeType;

View File

@ -0,0 +1,358 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.orm.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
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 static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* @author Gail Badner
*/
@DomainModel(
annotatedClasses = {
VersionedNoCascadeOne.class,
Many.class
}
)
@SessionFactory
public class VersionedNoCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testMergeNullCollection(SessionFactoryScope scope) {
VersionedNoCascadeOne versionedNoCascadeOne = new VersionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedNoCascadeOne.getManies() );
session.save( versionedNoCascadeOne );
assertNull( versionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = VersionedNoCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
VersionedNoCascadeOne one = (VersionedNoCascadeOne) session.merge( versionedNoCascadeOne );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( session, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndNullifyCollection(SessionFactoryScope scope) {
VersionedNoCascadeOne versionedNoCascadeOne = new VersionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedNoCascadeOne.getManies() );
session.save( versionedNoCascadeOne );
assertNull( versionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = VersionedNoCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
VersionedNoCascadeOne one = session.get(
VersionedNoCascadeOne.class,
versionedNoCascadeOne.getId()
);
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManies.getLoadedPersister()
);
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
@TestForIssue(jiraKey = "HHH-9777")
public void testGetAndReplaceCollection(SessionFactoryScope scope) {
VersionedNoCascadeOne versionedNoCascadeOne = new VersionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedNoCascadeOne.getManies() );
session.save( versionedNoCascadeOne );
assertNull( versionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
final String role = VersionedNoCascadeOne.class.getName() + ".manies";
scope.inTransaction(
session -> {
VersionedNoCascadeOne one = session.get(
VersionedNoCascadeOne.class,
versionedNoCascadeOne.getId()
);
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
EntityEntry eeOne = getEntityEntry( session, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue(
"manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( session, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( session, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesOrig.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<>() );
session.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( session, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( session, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry(
session,
(PersistentCollection) one.getManies()
);
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame(
scope.getSessionFactory().getCollectionPersister( role ),
ceManiesAfterReplace.getLoadedPersister()
);
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
}
);
}
@Test
public void testSaveOrUpdateNullCollection(SessionFactoryScope scope) {
VersionedNoCascadeOne versionedNoCascadeOne = new VersionedNoCascadeOne();
scope.inTransaction(
session -> {
assertNull( versionedNoCascadeOne.getManies() );
session.save( versionedNoCascadeOne );
assertNull( versionedNoCascadeOne.getManies() );
EntityEntry eeOne = getEntityEntry( session, versionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
assertNull( versionedNoCascadeOne.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
scope.inTransaction(
session -> {
session.saveOrUpdate( versionedNoCascadeOne );
// Ensure one.getManies() is still null.
assertNull( versionedNoCascadeOne.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
EntityEntry eeOne = getEntityEntry( session, versionedNoCascadeOne );
assertNull( eeOne.getLoadedValue( "manies" ) );
session.flush();
// Ensure one.getManies() is still null.
assertNull( versionedNoCascadeOne.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( session, versionedNoCascadeOne ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
}
);
}
}

View File

@ -21,10 +21,9 @@
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
package org.hibernate.orm.test.collection.dereferenced;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

View File

@ -1,335 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gail Badner
*/
public class UnversionedCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testMergeNullCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedCascadeOne one = new UnversionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = UnversionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedCascadeOne) s.merge( one );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
eeOne = getEntityEntry( s, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndNullifyCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedCascadeOne one = new UnversionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = UnversionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedCascadeOne) s.get( UnversionedCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManies.getLoadedPersister() );
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndReplaceCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedCascadeOne one = new UnversionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = UnversionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedCascadeOne) s.get( UnversionedCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<Many>() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( s, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry( s, (PersistentCollection) one.getManies() );
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesAfterReplace.getLoadedPersister() );
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
public void testSaveOrUpdateNullCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedCascadeOne one = new UnversionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
s.saveOrUpdate( one );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
UnversionedCascadeOne.class,
Many.class
};
}
}

View File

@ -1,334 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gail Badner
*/
public class UnversionedNoCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testMergeNullCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedNoCascadeOne one = new UnversionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedNoCascadeOne) s.merge( one );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
eeOne = getEntityEntry( s, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndNullifyCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedNoCascadeOne one = new UnversionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedNoCascadeOne) s.get( UnversionedNoCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManies.getLoadedPersister() );
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndReplaceCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedNoCascadeOne one = new UnversionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = UnversionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (UnversionedNoCascadeOne) s.get( UnversionedNoCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<Many>() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( s, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry( s, (PersistentCollection) one.getManies() );
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesAfterReplace.getLoadedPersister() );
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
public void testSaveOrUpdateNullCollection() {
Session s = openSession();
s.getTransaction().begin();
UnversionedNoCascadeOne one = new UnversionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
s.saveOrUpdate( one );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
UnversionedNoCascadeOne.class,
Many.class
};
}
}

View File

@ -1,334 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gail Badner
*/
public class VersionedCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testMergeNullCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedCascadeOne one = new VersionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = VersionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedCascadeOne) s.merge( one );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
eeOne = getEntityEntry( s, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndNullifyCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedCascadeOne one = new VersionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = VersionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedCascadeOne) s.get( VersionedCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManies.getLoadedPersister() );
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndReplaceCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedCascadeOne one = new VersionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role = VersionedCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedCascadeOne) s.get( VersionedCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<Many>() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( s, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry( s, (PersistentCollection) one.getManies() );
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesAfterReplace.getLoadedPersister() );
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
public void testSaveOrUpdateNullCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedCascadeOne one = new VersionedCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
s.saveOrUpdate( one );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
VersionedCascadeOne.class,
Many.class
};
}
}

View File

@ -1,334 +0,0 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.collection.dereferenced;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.collection.internal.AbstractPersistentCollection;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.CollectionEntry;
import org.hibernate.engine.spi.EntityEntry;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
/**
* @author Gail Badner
*/
public class VersionedNoCascadeDereferencedCollectionTest extends AbstractDereferencedCollectionTest {
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testMergeNullCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedNoCascadeOne one = new VersionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role =VersionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedNoCascadeOne) s.merge( one );
// after merging, one.getManies() should still be null;
// the EntityEntry loaded state should contain a PersistentCollection though.
assertNull( one.getManies() );
eeOne = getEntityEntry( s, one );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertNotNull( maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndNullifyCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedNoCascadeOne one = new VersionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role =VersionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedNoCascadeOne) s.get(VersionedNoCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManies = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManies );
assertEquals( role, ceManies.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManies.getLoadedPersister() );
assertEquals( one.getId(), ceManies.getKey() );
// nullify collection
one.setManies( null );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManies.getRole() );
assertNull( ceManies.getLoadedPersister() );
assertNull( ceManies.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// Ensure eeOne.getLoadedState() returns null for collection after flush.
assertNull( eeOne.getLoadedValue( "manies" ) );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9777" )
public void testGetAndReplaceCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedNoCascadeOne one = new VersionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
final String role =VersionedNoCascadeOne.class.getName() + ".manies";
s = openSession();
s.getTransaction().begin();
one = (VersionedNoCascadeOne) s.get(VersionedNoCascadeOne.class, one.getId() );
// When returned by Session.get(), one.getManies() will return a PersistentCollection;
// the EntityEntry loaded state should contain the same PersistentCollection.
eeOne = getEntityEntry( s, one );
assertNotNull( one.getManies() );
AbstractPersistentCollection maniesEEOneStateOrig = (AbstractPersistentCollection) eeOne.getLoadedValue( "manies" );
assertSame( one.getManies(), maniesEEOneStateOrig );
// Ensure maniesEEOneStateOrig has role, key, and session properly defined (even though one.manies == null)
assertEquals( role, maniesEEOneStateOrig.getRole() );
assertEquals( one.getId(), maniesEEOneStateOrig.getKey() );
assertSame( s, maniesEEOneStateOrig.getSession() );
// Ensure there is a CollectionEntry for maniesEEOneStateOrig and that the role, persister, and key are set properly.
CollectionEntry ceManiesOrig = getCollectionEntry( s, maniesEEOneStateOrig );
assertNotNull( ceManiesOrig );
assertEquals( role, ceManiesOrig.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesOrig.getLoadedPersister() );
assertEquals( one.getId(), ceManiesOrig.getKey() );
// replace collection
one.setManies( new HashSet<Many>() );
s.flush();
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure CollectionEntry for maniesEEOneStateOrig is no longer in the PersistenceContext.
assertNull( getCollectionEntry( s, maniesEEOneStateOrig ) );
// Ensure the original CollectionEntry has role, persister, and key set to null.
assertNull( ceManiesOrig.getRole() );
assertNull( ceManiesOrig.getLoadedPersister() );
assertNull( ceManiesOrig.getKey() );
// Ensure the PersistentCollection (that was previously returned by eeOne.getLoadedState())
// has key and role set to null.
assertNull( maniesEEOneStateOrig.getKey() );
assertNull( maniesEEOneStateOrig.getRole() );
// one.getManies() should be "wrapped" by a PersistentCollection now; role, key, and session should be set properly.
assertTrue( PersistentCollection.class.isInstance( one.getManies() ) );
assertEquals( role, ( (PersistentCollection) one.getManies() ).getRole() );
assertEquals( one.getId(), ( (PersistentCollection) one.getManies() ).getKey() );
assertSame( s, ( (AbstractPersistentCollection) one.getManies() ).getSession() );
// Ensure eeOne.getLoadedState() contains the new collection.
assertSame( one.getManies(), eeOne.getLoadedValue( "manies" ) );
// Ensure there is a new CollectionEntry for the new collection and that role, persister, and key are set properly.
CollectionEntry ceManiesAfterReplace = getCollectionEntry( s, (PersistentCollection) one.getManies() );
assertNotNull( ceManiesAfterReplace );
assertEquals( role, ceManiesAfterReplace.getRole() );
assertSame( sessionFactory().getCollectionPersister( role ), ceManiesAfterReplace.getLoadedPersister() );
assertEquals( one.getId(), ceManiesAfterReplace.getKey() );
// Ensure the session in maniesEEOneStateOrig has been unset.
assertNull( maniesEEOneStateOrig.getSession() );
s.getTransaction().commit();
s.close();
}
@Test
public void testSaveOrUpdateNullCollection() {
Session s = openSession();
s.getTransaction().begin();
VersionedNoCascadeOne one = new VersionedNoCascadeOne();
assertNull( one.getManies() );
s.save( one );
assertNull( one.getManies() );
EntityEntry eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
assertNull( one.getManies() );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
s.saveOrUpdate( one );
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the EntityEntry loaded state contains null for the manies collection.
eeOne = getEntityEntry( s, one );
assertNull( eeOne.getLoadedValue( "manies" ) );
s.flush();
// Ensure one.getManies() is still null.
assertNull( one.getManies() );
// Ensure the same EntityEntry is being used.
assertSame( eeOne, getEntityEntry( s, one ) );
// Ensure the EntityEntry loaded state still contains null for the manies collection.
assertNull( eeOne.getLoadedValue( "manies" ) );
s.getTransaction().commit();
s.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] {
VersionedNoCascadeOne.class,
Many.class
};
}
}