diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java index 7fb8071d54..8e2f504fb0 100644 --- a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/EnhancerTest.java @@ -15,6 +15,7 @@ import org.hibernate.test.bytecode.enhancement.association.OneToManyAssociationT import org.hibernate.test.bytecode.enhancement.association.OneToOneAssociationTestTask; import org.hibernate.test.bytecode.enhancement.basic.BasicEnhancementTestTask; import org.hibernate.test.bytecode.enhancement.basic.HHH9529TestTask; +import org.hibernate.test.bytecode.enhancement.cascade.CascadeDeleteTestTask; import org.hibernate.test.bytecode.enhancement.dirty.DirtyTrackingTestTask; import org.hibernate.test.bytecode.enhancement.extended.ExtendedAssociationManagementTestTasK; import org.hibernate.test.bytecode.enhancement.extended.ExtendedEnhancementTestTask; @@ -74,6 +75,12 @@ public class EnhancerTest extends BaseUnitTestCase { EnhancerTestUtils.runEnhancerTestTask( LazyBasicFieldAccessTestTask.class ); } + @Test + @TestForIssue( jiraKey = "HHH-10252" ) + public void testCascadeDelete() { + EnhancerTestUtils.runEnhancerTestTask( CascadeDeleteTestTask.class ); + } + @Test @TestForIssue( jiraKey = "HHH-10055" ) public void testLazyCollectionHandling() { diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/CascadeDeleteTestTask.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/CascadeDeleteTestTask.java new file mode 100644 index 0000000000..6720532162 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/CascadeDeleteTestTask.java @@ -0,0 +1,64 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.bytecode.enhancement.cascade; + +import org.hibernate.Session; +import org.hibernate.cfg.Configuration; +import org.hibernate.cfg.Environment; + +import org.hibernate.test.bytecode.enhancement.AbstractEnhancerTestTask; + +/** + * @author Luis Barreiro + */ +public class CascadeDeleteTestTask extends AbstractEnhancerTestTask { + + + public Class[] getAnnotatedClasses() { + return new Class[] {Parent.class, Child.class}; + } + + public void prepare() { + Configuration cfg = new Configuration(); + cfg.setProperty( Environment.ENABLE_LAZY_LOAD_NO_TRANS, "true" ); + cfg.setProperty( Environment.USE_SECOND_LEVEL_CACHE, "false" ); + super.prepare( cfg ); + + // Create a Parent with one Child + Session s = getFactory().openSession(); + s.beginTransaction(); + + Parent p = new Parent(); + p.setName("PARENT"); + p.setLazy("LAZY"); + + Child child = p.makeChild(); + s.persist(p); + + s.getTransaction().commit(); + s.close(); + } + + public void execute() { + // Delete the Parent + Session s = getFactory().openSession(); + s.beginTransaction(); + Parent loadedParent = (Parent) s.createQuery( "SELECT p FROM Parent p WHERE name=:name" ) + .setParameter( "name", "PARENT" ) + .uniqueResult(); + + s.delete( loadedParent ); + s.getTransaction().commit(); + s.close(); + // If the lazy relation is not fetch on cascade there is a constraint violation on commit + } + + protected void cleanup() { + } + + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/Child.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/Child.java new file mode 100644 index 0000000000..7b0cb94ddc --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/Child.java @@ -0,0 +1,39 @@ +package org.hibernate.test.bytecode.enhancement.cascade; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +/** + * Created by barreiro on 12/9/15. + */ +@Entity +public class Child { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @ManyToOne(optional = false) + @JoinColumn(name = "parent_id") + private Parent parent; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Parent getParent() { + return parent; + } + + public void setParent(Parent parent) { + this.parent = parent; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/Parent.java b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/Parent.java new file mode 100644 index 0000000000..a0d3757a2b --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/bytecode/enhancement/cascade/Parent.java @@ -0,0 +1,70 @@ +package org.hibernate.test.bytecode.enhancement.cascade; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.Basic; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +/** + * Created by barreiro on 12/9/15. + */ +@Entity +public class Parent { + private Long id; + private String name; + private List children = new ArrayList(); + private String lazy; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + @OneToMany(mappedBy = "parent", cascade = { + CascadeType.PERSIST, CascadeType.MERGE, + CascadeType.REFRESH, CascadeType.REMOVE + }, + fetch = FetchType.LAZY) + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Basic(fetch = FetchType.LAZY) + public String getLazy() { + return lazy; + } + + public void setLazy(String lazy) { + this.lazy = lazy; + } + + Child makeChild() { + final Child c = new Child(); + c.setParent( this ); + this.children.add( c ); + return c; + } +}