HHH-9091 : Cascaded collection delete fails due to wrong execution sequence (test case)

This commit is contained in:
Gail Badner 2014-12-02 22:06:53 -08:00
parent 1ff9edfb81
commit 3b57f1612d
10 changed files with 695 additions and 2 deletions

View File

@ -0,0 +1,170 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.bidirectional.multilevelcascade;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.test.orphan.one2one.fk.bidirectional.Employee;
import org.hibernate.test.orphan.one2one.fk.bidirectional.EmployeeInfo;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
* @author Gail Badner
*/
public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
private void createData() {
Preisregelung preisregelung = new Preisregelung();
preisregelung.setId( 17960L );
Tranchenmodell tranchenmodell = new Tranchenmodell();
tranchenmodell.setId( 1951L );
Tranche tranche1 = new Tranche();
tranche1.setId( 1951L);
Tranche tranche2 = new Tranche();
tranche2.setId( 1952L);
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
Session session = openSession();
session.beginTransaction();
session.save( preisregelung );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Tranche" ).executeUpdate();
session.createQuery( "delete Tranchenmodell" ).executeUpdate();
session.createQuery( "delete Preisregelung" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
@FailureExpected( jiraKey = "HHH-9091")
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
preisregelung.setTranchenmodell( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
preisregelung = ( Preisregelung ) session.get( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
@FailureExpected( jiraKey = "HHH-9091")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
// Replace with a new Tranchenmodell instance
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
tranchenmodellNew.setId( 1952L );
preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
Tranchenmodell tranchenmodellQueried = (Tranchenmodell) results.get( 0 );
assertEquals( tranchenmodellNew.getId(), tranchenmodellQueried.getId() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung1Queried = (Preisregelung) results.get( 0 );
assertEquals( tranchenmodellQueried, preisregelung1Queried.getTranchenmodell() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Preisregelung.class,
Tranche.class,
Tranchenmodell.class
};
}
}

View File

@ -0,0 +1,52 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.*;
@Entity
public class Preisregelung {
@Id
private Long id;
@OneToOne(mappedBy = "preisregelung", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Tranchenmodell tranchenmodell;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tranchenmodell getTranchenmodell() {
return tranchenmodell;
}
public void setTranchenmodell(Tranchenmodell tranchenmodell) {
this.tranchenmodell = tranchenmodell;
}
}

View File

@ -0,0 +1,53 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.*;
@Entity
public class Tranche {
@Id
private Long id;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Tranchenmodell tranchenmodell;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tranchenmodell getTranchenmodell() {
return tranchenmodell;
}
public void setTranchenmodell(Tranchenmodell tranchenmodell) {
this.tranchenmodell = tranchenmodell;
}
}

View File

@ -0,0 +1,65 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Tranchenmodell {
@Id
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tranchenmodell", fetch = FetchType.LAZY, orphanRemoval = true)
private List<Tranche> tranchen = new ArrayList<Tranche>();
@OneToOne(optional = true, fetch = FetchType.LAZY)
private Preisregelung preisregelung;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Tranche> getTranchen() {
return tranchen;
}
public Preisregelung getPreisregelung() {
return preisregelung;
}
public void setPreisregelung(Preisregelung preisregelung) {
this.preisregelung = preisregelung;
}
}

View File

@ -0,0 +1,165 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import java.util.List;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* @author Steve Ebersole
* @author Gail Badner
*/
public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
private void createData() {
Preisregelung preisregelung = new Preisregelung();
preisregelung.setId( 17960L );
Tranchenmodell tranchenmodell = new Tranchenmodell();
tranchenmodell.setId( 1951L );
Tranche tranche1 = new Tranche();
tranche1.setId( 1951L);
Tranche tranche2 = new Tranche();
tranche2.setId( 1952L);
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
Session session = openSession();
session.beginTransaction();
session.save( preisregelung );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete Tranche" ).executeUpdate();
session.createQuery( "delete Preisregelung" ).executeUpdate();
session.createQuery( "delete Tranchenmodell" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
preisregelung.setTranchenmodell( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
preisregelung = (Preisregelung) session.get( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testReplacedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() );
// Replace with a new Tranchenmodell instance
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
tranchenmodellNew.setId( 1952L );
preisregelung.setTranchenmodell( tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
Tranchenmodell tranchenmodellQueried = (Tranchenmodell) results.get( 0 );
assertEquals( tranchenmodellNew.getId(), tranchenmodellQueried.getId() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
Preisregelung preisregelung1Queried = (Preisregelung) results.get( 0 );
assertEquals( tranchenmodellQueried, preisregelung1Queried.getTranchenmodell() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Preisregelung.class,
Tranche.class,
Tranchenmodell.class
};
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Preisregelung {
@Id
private Long id;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Tranchenmodell tranchenmodell;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tranchenmodell getTranchenmodell() {
return tranchenmodell;
}
public void setTranchenmodell(Tranchenmodell tranchenmodell) {
this.tranchenmodell = tranchenmodell;
}
}

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Tranche {
@Id
private Long id;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Tranchenmodell tranchenmodell;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tranchenmodell getTranchenmodell() {
return tranchenmodell;
}
public void setTranchenmodell(Tranchenmodell tranchenmodell) {
this.tranchenmodell = tranchenmodell;
}
}

View File

@ -0,0 +1,70 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2014, 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.orphan.one2one.fk.reversed.bidirectional.multilevelcascade;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@Entity
public class Tranchenmodell {
@Id
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tranchenmodell", fetch = FetchType.LAZY, orphanRemoval = true)
private List<Tranche> tranchen = new ArrayList<Tranche>();
@OneToOne(mappedBy="tranchenmodell", optional = true, fetch = FetchType.LAZY)
private Preisregelung preisregelung;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Tranche> getTranchen() {
return tranchen;
}
public Preisregelung getPreisregelung() {
return preisregelung;
}
public void setPreisregelung(Preisregelung preisregelung) {
this.preisregelung = preisregelung;
}
}

View File

@ -78,7 +78,7 @@ public class DeleteOneToManyOrphansTest extends BaseCoreFunctionalTestCase {
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Product product = ( Product ) results.get( 0 ); Product product = ( Product ) results.get( 0 );
assertEquals( 1, product.getFeatures().size() ); assertEquals( 1, product.getFeatures().size() );
product.getFeatures().clear();; product.getFeatures().clear();
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
@ -132,7 +132,6 @@ public class DeleteOneToManyOrphansTest extends BaseCoreFunctionalTestCase {
cleanupData(); cleanupData();
} }
@Test @Test
@TestForIssue( jiraKey = "HHH-9330") @TestForIssue( jiraKey = "HHH-9330")
public void testReplacedWhileManaged() { public void testReplacedWhileManaged() {

View File

@ -41,6 +41,13 @@ public class Feature {
private String name; private String name;
public Feature() {
}
public Feature(Product product) {
this.product = product;
}
public Long getId() { public Long getId() {
return id; return id;
} }