HHH-9091 : Collection deleted due to orphan removal fails with constraint violation

This commit is contained in:
Gail Badner 2015-01-20 23:01:51 -08:00
parent 2d64e539aa
commit f1a9a9f006
14 changed files with 958 additions and 28 deletions

View File

@ -145,6 +145,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
private int cascading; private int cascading;
private int loadCounter; private int loadCounter;
private int removeOrphanBeforeUpdatesCounter;
private boolean flushing; private boolean flushing;
private boolean defaultReadOnly; private boolean defaultReadOnly;
@ -1029,6 +1030,42 @@ public class StatefulPersistenceContext implements PersistenceContext {
} }
} }
public boolean isRemovingOrphanBeforeUpates() {
return removeOrphanBeforeUpdatesCounter > 0;
}
public void beginRemoveOrphanBeforeUpdates() {
if ( getCascadeLevel() < 1 ) {
throw new IllegalStateException( "Attempt to remove orphan when not cascading." );
}
if ( removeOrphanBeforeUpdatesCounter >= getCascadeLevel() ) {
throw new IllegalStateException(
String.format(
"Cascade level [%d] is out of sync with removeOrphanBeforeUpdatesCounter [%d] before incrementing removeOrphanBeforeUpdatesCounter",
getCascadeLevel(),
removeOrphanBeforeUpdatesCounter
)
);
}
removeOrphanBeforeUpdatesCounter++;
}
public void endRemoveOrphanBeforeUpdates() {
if ( getCascadeLevel() < 1 ) {
throw new IllegalStateException( "Finished removing orphan when not cascading." );
}
if ( removeOrphanBeforeUpdatesCounter > getCascadeLevel() ) {
throw new IllegalStateException(
String.format(
"Cascade level [%d] is out of sync with removeOrphanBeforeUpdatesCounter [%d] before decrementing removeOrphanBeforeUpdatesCounter",
getCascadeLevel(),
removeOrphanBeforeUpdatesCounter
)
);
}
removeOrphanBeforeUpdatesCounter--;
}
/** /**
* Call this before beginning a two-phase load * Call this before beginning a two-phase load
*/ */
@ -1369,7 +1406,7 @@ public class StatefulPersistenceContext implements PersistenceContext {
public void serialize(ObjectOutputStream oos) throws IOException { public void serialize(ObjectOutputStream oos) throws IOException {
final boolean tracing = LOG.isTraceEnabled(); final boolean tracing = LOG.isTraceEnabled();
if ( tracing ) { if ( tracing ) {
LOG.trace( "Serializing persistence-context" ); LOG.trace( "Serializing persisatence-context" );
} }
oos.writeBoolean( defaultReadOnly ); oos.writeBoolean( defaultReadOnly );

View File

@ -903,14 +903,50 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc
@Override @Override
public void delete(String entityName, Object object, boolean isCascadeDeleteEnabled, Set transientEntities) throws HibernateException { public void delete(String entityName, Object object, boolean isCascadeDeleteEnabled, Set transientEntities) throws HibernateException {
fireDelete( new DeleteEvent( entityName, object, isCascadeDeleteEnabled, this ), transientEntities ); if ( TRACE_ENABLED && persistenceContext.isRemovingOrphanBeforeUpates() ) {
logRemoveOrphanBeforeUpdates( "before continuing", entityName, object );
}
fireDelete(
new DeleteEvent(
entityName,
object,
isCascadeDeleteEnabled,
persistenceContext.isRemovingOrphanBeforeUpates(),
this
),
transientEntities
);
if ( TRACE_ENABLED && persistenceContext.isRemovingOrphanBeforeUpates() ) {
logRemoveOrphanBeforeUpdates( "after continuing", entityName, object );
}
} }
@Override @Override
public void removeOrphanBeforeUpdates(String entityName, Object child) { public void removeOrphanBeforeUpdates(String entityName, Object child) {
// TODO: The removeOrphan concept is a temporary "hack" for HHH-6484. This should be removed once action/task // TODO: The removeOrphan concept is a temporary "hack" for HHH-6484. This should be removed once action/task
// ordering is improved. // ordering is improved.
fireDelete( new DeleteEvent( entityName, child, false, true, this ) ); if ( TRACE_ENABLED ) {
logRemoveOrphanBeforeUpdates( "begin", entityName, child );
}
persistenceContext.beginRemoveOrphanBeforeUpdates();
try {
fireDelete( new DeleteEvent( entityName, child, false, true, this ) );
}
finally {
persistenceContext.endRemoveOrphanBeforeUpdates();
if ( TRACE_ENABLED ) {
logRemoveOrphanBeforeUpdates( "end", entityName, child );
}
}
}
private void logRemoveOrphanBeforeUpdates(String timing, String entityName, Object entity) {
final EntityEntry entityEntry = persistenceContext.getEntry( entity );
LOG.tracef(
"%s remove orphan before updates: [%s]",
timing,
entityEntry == null ? entityName : MessageHelper.infoString( entityName, entityEntry.getId() )
);
} }
private void fireDelete(DeleteEvent event) { private void fireDelete(DeleteEvent event) {

View File

@ -28,9 +28,6 @@ import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.hibernate.Session; 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.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
@ -46,25 +43,31 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
private void createData() { private void createData() {
Preisregelung preisregelung = new Preisregelung(); Preisregelung preisregelung = new Preisregelung();
preisregelung.setId( 17960L );
Tranchenmodell tranchenmodell = new Tranchenmodell(); Tranchenmodell tranchenmodell = new Tranchenmodell();
tranchenmodell.setId( 1951L );
X x = new X();
Tranche tranche1 = new Tranche(); Tranche tranche1 = new Tranche();
tranche1.setId( 1951L);
Y y = new Y();
Tranche tranche2 = new Tranche(); Tranche tranche2 = new Tranche();
tranche2.setId( 1952L);
preisregelung.setTranchenmodell( tranchenmodell ); preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung ); tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.setX( x );
x.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche1 ); tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell ); tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 ); tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell ); tranche2.setTranchenmodell( tranchenmodell );
tranche1.setY( y );
y.setTranche( tranche1 );
Session session = openSession(); Session session = openSession();
session.beginTransaction(); session.beginTransaction();
session.save( preisregelung ); session.save( preisregelung );
@ -84,8 +87,7 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
@Test @Test
@TestForIssue( jiraKey = "HHH-9091") @TestForIssue( jiraKey = "HHH-9091")
@FailureExpected( jiraKey = "HHH-9091") public void testDirectAssociationOrphanedWhileManaged() {
public void testOrphanedWhileManaged() {
createData(); createData();
Session session = openSession(); Session session = openSession();
@ -95,7 +97,11 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
results = session.createQuery( "from Preisregelung" ).list(); results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 ); Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() ); Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null ); preisregelung.setTranchenmodell( null );
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
@ -107,6 +113,13 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
assertNull( preisregelung.getTranchenmodell() ); assertNull( preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranchenmodell" ).list(); results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 0, results.size() ); assertEquals( 0, results.size() );
results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list(); results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
@ -115,11 +128,10 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
cleanupData(); cleanupData();
} }
@Test @Test
@TestForIssue( jiraKey = "HHH-9091") @TestForIssue( jiraKey = "HHH-9091")
@FailureExpected( jiraKey = "HHH-9091") public void testReplacedDirectAssociationWhileManaged() {
public void testReplacedWhileManaged() {
createData(); createData();
Session session = openSession(); Session session = openSession();
@ -129,11 +141,56 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
results = session.createQuery( "from Preisregelung" ).list(); results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Preisregelung preisregelung = ( Preisregelung ) results.get( 0 ); Preisregelung preisregelung = ( Preisregelung ) results.get( 0 );
assertNotNull( preisregelung.getTranchenmodell() ); Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
// Replace with a new Tranchenmodell instance // Create a new Tranchenmodell with new direct and nested associations
Tranchenmodell tranchenmodellNew = new Tranchenmodell(); Tranchenmodell tranchenmodellNew = new Tranchenmodell();
tranchenmodellNew.setId( 1952L ); X xNew = new X();
tranchenmodellNew.setX( xNew );
xNew.setTranchenmodell( tranchenmodellNew );
Tranche trancheNew = new Tranche();
tranchenmodellNew.getTranchen().add( trancheNew );
trancheNew.setTranchenmodell( tranchenmodellNew );
Y yNew = new Y();
trancheNew.setY( yNew );
yNew.setTranche( trancheNew );
// Replace with a new Tranchenmodell instance containing new direct and nested associations
preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
results = session.createQuery( "from Tranche" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
preisregelung = ( Preisregelung ) results.get( 0 );
tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
assertNotNull( tranchenmodell.getX() );
assertEquals( xNew.getId(), tranchenmodell.getX().getId() );
assertEquals( 1, tranchenmodell.getTranchen().size() );
assertEquals( trancheNew.getId(), tranchenmodell.getTranchen().get( 0 ).getId() );
assertEquals( yNew.getId(), tranchenmodell.getTranchen().get( 0 ).getY().getId() );
// Replace with a new Tranchenmodell instance with no associations
tranchenmodellNew = new Tranchenmodell();
preisregelung.setTranchenmodell(tranchenmodellNew ); preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung ); tranchenmodellNew.setPreisregelung( preisregelung );
session.getTransaction().commit(); session.getTransaction().commit();
@ -143,14 +200,63 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
session.beginTransaction(); session.beginTransaction();
results = session.createQuery( "from Tranchenmodell" ).list(); results = session.createQuery( "from Tranchenmodell" ).list();
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Tranchenmodell tranchenmodellQueried = (Tranchenmodell) results.get( 0 ); tranchenmodell = (Tranchenmodell) results.get( 0 );
assertEquals( tranchenmodellNew.getId(), tranchenmodellQueried.getId() ); assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
results = session.createQuery( "from Preisregelung" ).list(); results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() ); assertEquals( 1, results.size() );
Preisregelung preisregelung1Queried = (Preisregelung) results.get( 0 ); preisregelung = (Preisregelung) results.get( 0 );
assertEquals( tranchenmodellQueried, preisregelung1Queried.getTranchenmodell() ); assertEquals( tranchenmodell, preisregelung.getTranchenmodell() );
results = session.createQuery( "from Tranche" ).list(); results = session.createQuery( "from Tranche" ).list();
assertEquals( 0, results.size() ); assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testDirectAndNestedAssociationsOrphanedWhileManaged() {
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 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
tranchenmodell.setX( null );
tranchenmodell.getTranchen().get( 0 ).setY( 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 Tranche" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from X" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Y" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Preisregelung" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit(); session.getTransaction().commit();
session.close(); session.close();
@ -163,7 +269,9 @@ public class DeleteMultiLevelOrphansTest extends BaseCoreFunctionalTestCase {
return new Class[]{ return new Class[]{
Preisregelung.class, Preisregelung.class,
Tranche.class, Tranche.class,
Tranchenmodell.class Tranchenmodell.class,
X.class,
Y.class
}; };
} }

View File

@ -28,6 +28,7 @@ import javax.persistence.*;
@Entity @Entity
public class Preisregelung { public class Preisregelung {
@Id @Id
@GeneratedValue
private Long id; private Long id;
@OneToOne(mappedBy = "preisregelung", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true) @OneToOne(mappedBy = "preisregelung", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)

View File

@ -29,13 +29,16 @@ import javax.persistence.*;
public class Tranche { public class Tranche {
@Id @Id
private Long id; @GeneratedValue
private Long id;
@ManyToOne(optional = false, fetch = FetchType.EAGER) @ManyToOne(optional = false, fetch = FetchType.EAGER)
private Tranchenmodell tranchenmodell; private Tranchenmodell tranchenmodell;
@OneToOne(mappedBy = "tranche", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Y y;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -50,4 +53,12 @@ public class Tranche {
public void setTranchenmodell(Tranchenmodell tranchenmodell) { public void setTranchenmodell(Tranchenmodell tranchenmodell) {
this.tranchenmodell = tranchenmodell; this.tranchenmodell = tranchenmodell;
} }
public Y getY() {
return y;
}
public void setY(Y y) {
this.y = y;
}
} }

View File

@ -31,15 +31,18 @@ import java.util.List;
public class Tranchenmodell { public class Tranchenmodell {
@Id @Id
private Long id; @GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tranchenmodell", fetch = FetchType.LAZY, orphanRemoval = true) @OneToMany(cascade = CascadeType.ALL, mappedBy = "tranchenmodell", fetch = FetchType.LAZY, orphanRemoval = true)
@OrderColumn
private List<Tranche> tranchen = new ArrayList<Tranche>(); private List<Tranche> tranchen = new ArrayList<Tranche>();
@OneToOne(optional = true, fetch = FetchType.LAZY) @OneToOne(optional = true, fetch = FetchType.LAZY)
private Preisregelung preisregelung; private Preisregelung preisregelung;
@OneToOne(mappedBy = "tranchenmodell", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private X x;
public Long getId() { public Long getId() {
return id; return id;
@ -61,5 +64,11 @@ public class Tranchenmodell {
this.preisregelung = preisregelung; this.preisregelung = preisregelung;
} }
public X getX() {
return x;
}
public void setX(X x) {
this.x = x;
}
} }

View File

@ -0,0 +1,60 @@
/*
* 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.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
* @author Gail Badner
*/
@Entity
public class X {
@Id
@GeneratedValue
private Long id;
@OneToOne(optional = true, fetch = FetchType.LAZY)
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,60 @@
/*
* 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.orphan.one2one.fk.bidirectional.multilevelcascade;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
* @author Gail Badner
*/
@Entity
public class Y {
@Id
@GeneratedValue
private Long id;
@OneToOne(optional = true, fetch = FetchType.LAZY)
private Tranche tranche;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tranche getTranche() {
return tranche;
}
public void setTranche(Tranche tranche) {
this.tranche = tranche;
}
}

View File

@ -0,0 +1,279 @@
/*
* 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.jpa.test.orphan.onetoone.multilevelcascade;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.Test;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
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 BaseEntityManagerFunctionalTestCase {
private void createData() {
Preisregelung preisregelung = new Preisregelung();
Tranchenmodell tranchenmodell = new Tranchenmodell();
X x = new X();
Tranche tranche1 = new Tranche();
Y y = new Y();
Tranche tranche2 = new Tranche();
preisregelung.setTranchenmodell( tranchenmodell );
tranchenmodell.setPreisregelung( preisregelung );
tranchenmodell.setX( x );
x.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche1 );
tranche1.setTranchenmodell( tranchenmodell );
tranchenmodell.getTranchen().add( tranche2 );
tranche2.setTranchenmodell( tranchenmodell );
tranche1.setY( y );
y.setTranche( tranche1 );
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.persist( preisregelung );
em.getTransaction().commit();
em.close();
}
private void cleanupData() {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
em.createQuery( "delete Tranche" ).executeUpdate();
em.createQuery( "delete Tranchenmodell" ).executeUpdate();
em.createQuery( "delete Preisregelung" ).executeUpdate();
em.getTransaction().commit();
em.close();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testDirectAssociationOrphanedWhileManaged() {
createData();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
List results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
preisregelung = (Preisregelung) em.find( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Tranche" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from X" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Y" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
em.getTransaction().commit();
em.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testReplacedDirectAssociationWhileManaged() {
createData();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
List results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
// Create a new Tranchenmodell with new direct and nested associations
Tranchenmodell tranchenmodellNew = new Tranchenmodell();
X xNew = new X();
tranchenmodellNew.setX( xNew );
xNew.setTranchenmodell( tranchenmodellNew );
Tranche trancheNew = new Tranche();
tranchenmodellNew.getTranchen().add( trancheNew );
trancheNew.setTranchenmodell( tranchenmodellNew );
Y yNew = new Y();
trancheNew.setY( yNew );
yNew.setTranche( trancheNew );
// Replace with a new Tranchenmodell instance containing new direct and nested associations
preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
results = em.createQuery( "from Tranche" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from X" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from Y" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
preisregelung = (Preisregelung) results.get( 0 );
tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
assertNotNull( tranchenmodell.getX() );
assertEquals( xNew.getId(), tranchenmodell.getX().getId() );
assertEquals( 1, tranchenmodell.getTranchen().size() );
assertEquals( trancheNew.getId(), tranchenmodell.getTranchen().get( 0 ).getId() );
assertEquals( yNew.getId(), tranchenmodell.getTranchen().get( 0 ).getY().getId() );
// Replace with a new Tranchenmodell instance with no associations
tranchenmodellNew = new Tranchenmodell();
preisregelung.setTranchenmodell(tranchenmodellNew );
tranchenmodellNew.setPreisregelung( preisregelung );
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 1, results.size() );
tranchenmodell = (Tranchenmodell) results.get( 0 );
assertEquals( tranchenmodellNew.getId(), tranchenmodell.getId() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
preisregelung = (Preisregelung) results.get( 0 );
assertEquals( tranchenmodell, preisregelung.getTranchenmodell() );
results = em.createQuery( "from Tranche" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from X" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Y" ).getResultList();
assertEquals( 0, results.size() );
em.getTransaction().commit();
em.close();
cleanupData();
}
@Test
@TestForIssue( jiraKey = "HHH-9091")
public void testDirectAndNestedAssociationsOrphanedWhileManaged() {
createData();
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
List results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 1, results.size() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
Preisregelung preisregelung = (Preisregelung) results.get( 0 );
Tranchenmodell tranchenmodell = preisregelung.getTranchenmodell();
assertNotNull( tranchenmodell );
assertNotNull( tranchenmodell.getX() );
assertEquals( 2, tranchenmodell.getTranchen().size() );
assertNotNull( tranchenmodell.getTranchen().get( 0 ).getY() );
preisregelung.setTranchenmodell( null );
tranchenmodell.setX( null );
tranchenmodell.getTranchen().get( 0 ).setY( null );
em.getTransaction().commit();
em.close();
em = getOrCreateEntityManager();
em.getTransaction().begin();
preisregelung = (Preisregelung) em.find( Preisregelung.class, preisregelung.getId() );
assertNull( preisregelung.getTranchenmodell() );
results = em.createQuery( "from Tranchenmodell" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Tranche" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from X" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Y" ).getResultList();
assertEquals( 0, results.size() );
results = em.createQuery( "from Preisregelung" ).getResultList();
assertEquals( 1, results.size() );
em.getTransaction().commit();
em.close();
cleanupData();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
Preisregelung.class,
Tranche.class,
Tranchenmodell.class,
X.class,
Y.class
};
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.jpa.test.orphan.onetoone.multilevelcascade;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class Preisregelung {
@Id
@GeneratedValue
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,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.jpa.test.orphan.onetoone.multilevelcascade;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
@Entity
public class Tranche {
@Id
@GeneratedValue
private Long id;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
private Tranchenmodell tranchenmodell;
@OneToOne(mappedBy = "tranche", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Y y;
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;
}
public Y getY() {
return y;
}
public void setY(Y y) {
this.y = y;
}
}

View File

@ -0,0 +1,81 @@
/*
* 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.jpa.test.orphan.onetoone.multilevelcascade;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.OrderColumn;
@Entity
public class Tranchenmodell {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tranchenmodell", fetch = FetchType.LAZY, orphanRemoval = true)
@OrderColumn
private List<Tranche> tranchen = new ArrayList<Tranche>();
@OneToOne(optional = true, fetch = FetchType.LAZY)
private Preisregelung preisregelung;
@OneToOne(mappedBy = "tranchenmodell", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private X x;
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;
}
public X getX() {
return x;
}
public void setX(X x) {
this.x = x;
}
}

View File

@ -0,0 +1,60 @@
/*
* 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.jpa.test.orphan.onetoone.multilevelcascade;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
* @author Gail Badner
*/
@Entity
public class X {
@Id
@GeneratedValue
private Long id;
@OneToOne(optional = true, fetch = FetchType.LAZY)
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,60 @@
/*
* 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.jpa.test.orphan.onetoone.multilevelcascade;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
/**
* @author Gail Badner
*/
@Entity
public class Y {
@Id
@GeneratedValue
private Long id;
@OneToOne(optional = true, fetch = FetchType.LAZY)
private Tranche tranche;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Tranche getTranche() {
return tranche;
}
public void setTranche(Tranche tranche) {
this.tranche = tranche;
}
}