HHH-8550 - Order of remove/delete calls important for associated data

This commit is contained in:
Steve Ebersole 2013-09-25 13:19:49 -05:00
parent 185fa1d68b
commit f2b272f468
2 changed files with 37 additions and 70 deletions

View File

@ -63,6 +63,11 @@ public class RemoveOrderingTest extends BaseCoreFunctionalTestCase {
session.delete( company ); session.delete( company );
session.delete( person ); session.delete( person );
session.flush();
session.persist( person );
session.flush();
session.getTransaction().commit(); session.getTransaction().commit();
} }
catch (Exception e) { catch (Exception e) {

View File

@ -26,99 +26,61 @@ package org.hibernate.jpa.test.ops;
import javax.persistence.CascadeType; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.hibernate.Session; import org.hibernate.jpa.AvailableSettings;
import org.hibernate.SessionFactory; import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.boot.registry.BootstrapServiceRegistry;
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.event.spi.JpaIntegrator;
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.PersistentClass;
import org.hibernate.mapping.Property;
import org.hibernate.mapping.Selectable;
import org.junit.Test; import org.junit.Test;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue; import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/** /**
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class RemoveOrderingTest extends BaseUnitTestCase { public class RemoveOrderingTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected void addConfigOptions(Map options) {
super.addConfigOptions( options );
options.put( AvailableSettings.VALIDATION_MODE, "NONE" );
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Person.class, Company.class };
}
@Test @Test
@TestForIssue( jiraKey="HHH-8550" ) @TestForIssue( jiraKey = "HHH-8550" )
public void testManyToOne() { @FailureExpected( jiraKey = "HHH-8550" )
Configuration cfg = new Configuration() public void testManyToOne() throws Exception {
.addAnnotatedClass( Company.class ) EntityManager em = getOrCreateEntityManager();
.addAnnotatedClass( Person.class ) em.getTransaction().begin();
.setProperty( AvailableSettings.CHECK_NULLABILITY, "true" )
.setProperty( AvailableSettings.URL, "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;MVCC=TRUE;LOCK_TIMEOUT=10000" )
.setProperty( AvailableSettings.USER, "sa" )
.setProperty( AvailableSettings.DRIVER, org.h2.Driver.class.getName() )
.setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" );
cfg.buildMappings();
// Iterator<PersistentClass> classMappings = cfg.getClassMappings();
// while ( classMappings.hasNext() ) {
// PersistentClass pc = classMappings.next();
// if ( pc.getMappedClass().equals( Person.class ) ) {
// Property prop = pc.getProperty( "employer" );
// Iterator<Selectable> selectables = prop.getValue().getColumnIterator();
// while ( selectables.hasNext() ) {
// Column column = (Column) selectables.next();
// column.setNullable( true );
// }
// }
// }
BootstrapServiceRegistry bootstrapRegistry = new BootstrapServiceRegistryBuilder().with( new JpaIntegrator() ).build();
StandardServiceRegistry registry = new StandardServiceRegistryBuilder( bootstrapRegistry )
.applySettings( cfg.getProperties() )
.build();
SessionFactory sf = cfg.buildSessionFactory( registry );
try { try {
Session session = sf.openSession();
session.beginTransaction();
Company company = new Company( 1, "acme" ); Company company = new Company( 1, "acme" );
Person person = new Person( 1, "joe", company ); Person person = new Person( 1, "joe", company );
session.persist( person ); em.persist( person );
session.flush(); em.flush();
Company company2 = person.employer; em.remove( company );
em.remove( person );
em.flush();
session.delete( company2 ); em.persist( person );
session.delete( person ); em.flush();
session.flush();
session.persist( person ); em.getTransaction().commit();
session.flush();
session.getTransaction().commit();
session.close();
} }
finally { catch (Exception e) {
sf.close(); em.getTransaction().rollback();
throw e;
} }
em.close();
} }
@Entity( name="Company" ) @Entity( name="Company" )