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( person );
session.flush();
session.persist( person );
session.flush();
session.getTransaction().commit();
}
catch (Exception e) {

View File

@ -26,99 +26,61 @@ package org.hibernate.jpa.test.ops;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
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 org.hibernate.Session;
import org.hibernate.SessionFactory;
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.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.junit.Test;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseUnitTestCase;
/**
* @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
@TestForIssue( jiraKey="HHH-8550" )
public void testManyToOne() {
Configuration cfg = new Configuration()
.addAnnotatedClass( Company.class )
.addAnnotatedClass( Person.class )
.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 );
@TestForIssue( jiraKey = "HHH-8550" )
@FailureExpected( jiraKey = "HHH-8550" )
public void testManyToOne() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
Session session = sf.openSession();
session.beginTransaction();
Company company = new Company( 1, "acme" );
Person person = new Person( 1, "joe", company );
session.persist( person );
session.flush();
em.persist( person );
em.flush();
Company company2 = person.employer;
em.remove( company );
em.remove( person );
em.flush();
session.delete( company2 );
session.delete( person );
session.flush();
em.persist( person );
em.flush();
session.persist( person );
session.flush();
session.getTransaction().commit();
session.close();
em.getTransaction().commit();
}
finally {
sf.close();
catch (Exception e) {
em.getTransaction().rollback();
throw e;
}
em.close();
}
@Entity( name="Company" )