HHH-5299 : Multi-Level cascading of unsaved instances with bidirectional associations fails with TransientObjectException

This commit is contained in:
Gail Badner 2012-01-26 19:33:37 -08:00
parent 253cb0a9d3
commit bb4738b1e4
5 changed files with 296 additions and 1 deletions

View File

@ -393,7 +393,12 @@ public abstract class CascadingAction {
private boolean isInManagedState(Object child, EventSource session) {
EntityEntry entry = session.getPersistenceContext().getEntry( child );
return entry != null && (entry.getStatus() == Status.MANAGED || entry.getStatus() == Status.READ_ONLY);
return entry != null &&
(
entry.getStatus() == Status.MANAGED ||
entry.getStatus() == Status.READ_ONLY ||
entry.getStatus() == Status.SAVING
);
}
@Override

View File

@ -0,0 +1,56 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.ejb.test.cascade.multilevel;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "BOTTOM")
public class Bottom {
@Id
@GeneratedValue
private Long id;
@OneToOne(mappedBy = "bottom")
private Middle middle;
Long getId() {
return id;
}
void setId(Long id) {
this.id = id;
}
Middle getMiddle() {
return middle;
}
void setMiddle(Middle middle) {
this.middle = middle;
}
}

View File

@ -0,0 +1,78 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.ejb.test.cascade.multilevel;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "MIDDLE")
public class Middle {
@Id
private Long id;
@ManyToOne
private Top top;
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "BOTTOM_ID")
private Bottom bottom;
private Middle() {
}
public Middle(Long i) {
this.id = i;
}
Long getId() {
return id;
}
void setId(Long id) {
this.id = id;
}
Top getTop() {
return top;
}
void setTop(Top top) {
this.top = top;
}
Bottom getBottom() {
return bottom;
}
void setBottom(Bottom bottom) {
this.bottom = bottom;
bottom.setMiddle(this);
}
}

View File

@ -0,0 +1,87 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.ejb.test.cascade.multilevel;
import static org.junit.Assert.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import org.junit.Test;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
public class MultiLevelCascadeTest extends BaseEntityManagerFunctionalTestCase {
@TestForIssue( jiraKey = "HHH-5299" )
@Test
public void test() {
EntityManager em = getOrCreateEntityManager( );
EntityTransaction tx = em.getTransaction();
tx.begin();
Top top = new Top();
em.persist( top );
// Flush 1
em.flush();
Middle middle = new Middle( 1l );
top.addMiddle( middle );
middle.setTop( top );
Bottom bottom = new Bottom();
middle.setBottom( bottom );
bottom.setMiddle( middle );
Middle middle2 = new Middle( 2l );
top.addMiddle(middle2);
middle2.setTop( top );
Bottom bottom2 = new Bottom();
middle2.setBottom( bottom2 );
bottom2.setMiddle( middle2 );
// Flush 2
em.flush();
tx.commit();
em.close();
em = getOrCreateEntityManager();
tx = em.getTransaction();
tx.begin();
top = em.find(Top.class, top.getId());
assertEquals(2, top.getMiddles().size());
for (Middle loadedMiddle : top.getMiddles()) {
assertSame(top, loadedMiddle.getTop());
assertNotNull(loadedMiddle.getBottom());
}
em.remove( top );
em.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { Top.class, Middle.class, Bottom.class };
}
}

View File

@ -0,0 +1,69 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2012, 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.ejb.test.cascade.multilevel;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "TOP")
public class Top {
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "top")
List<Middle> middles;
Long getId() {
return id;
}
void setId(Long id) {
this.id = id;
}
List<Middle> getMiddles() {
if (middles == null) {
middles = new ArrayList<Middle>();
}
return middles;
}
void setMiddles(List<Middle> middles) {
this.middles = middles;
}
void addMiddle(Middle middle) {
this.getMiddles().add(middle);
middle.setTop(this);
}
}