HHH-10252 - Add test case

This commit is contained in:
barreiro 2015-12-09 04:41:31 +00:00 committed by Steve Ebersole
parent 328fa2363a
commit 601bdaa678
4 changed files with 180 additions and 0 deletions

View File

@ -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() {

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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() {
}
}

View File

@ -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;
}
}

View File

@ -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<Child> children = new ArrayList<Child>();
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<Child> getChildren() {
return children;
}
public void setChildren(List<Child> 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;
}
}