HHH-1594 - Add on-delete="cascade" support to unidirectional *ToOne relationships.

This commit is contained in:
Vlad Mihalcea 2016-06-01 12:30:17 +03:00
parent c579c8b451
commit 545930c39f
9 changed files with 298 additions and 2 deletions

View File

@ -9,6 +9,7 @@ package org.hibernate.boot.model.source.internal.hbm;
import java.util.List; import java.util.List;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmManyToOneType; import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmManyToOneType;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmOnDeleteEnum;
import org.hibernate.boot.model.JavaTypeDescriptor; import org.hibernate.boot.model.JavaTypeDescriptor;
import org.hibernate.boot.model.source.spi.AttributePath; import org.hibernate.boot.model.source.spi.AttributePath;
import org.hibernate.boot.model.source.spi.AttributeRole; import org.hibernate.boot.model.source.spi.AttributeRole;
@ -191,7 +192,7 @@ class SingularAttributeSourceManyToOneImpl
@Override @Override
public boolean isCascadeDeleteEnabled() { public boolean isCascadeDeleteEnabled() {
return false; return JaxbHbmOnDeleteEnum.CASCADE.equals( manyToOneElement.getOnDelete() );
} }
@Override @Override

View File

@ -9,6 +9,7 @@ package org.hibernate.boot.model.source.internal.hbm;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmOnDeleteEnum;
import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmOneToOneType; import org.hibernate.boot.jaxb.hbm.spi.JaxbHbmOneToOneType;
import org.hibernate.boot.model.JavaTypeDescriptor; import org.hibernate.boot.model.JavaTypeDescriptor;
import org.hibernate.boot.model.source.spi.AttributePath; import org.hibernate.boot.model.source.spi.AttributePath;
@ -193,7 +194,7 @@ class SingularAttributeSourceOneToOneImpl
@Override @Override
public boolean isCascadeDeleteEnabled() { public boolean isCascadeDeleteEnabled() {
return false; return JaxbHbmOnDeleteEnum.CASCADE.equals( oneToOneElement.getOnDelete() );
} }
@Override @Override

View File

@ -986,6 +986,7 @@
<xs:attribute name="node" type="xs:string"/> <xs:attribute name="node" type="xs:string"/>
<xs:attribute name="outer-join" type="OuterJoinEnum"/> <xs:attribute name="outer-join" type="OuterJoinEnum"/>
<xs:attribute name="property-ref" type="xs:string"/> <xs:attribute name="property-ref" type="xs:string"/>
<xs:attribute name="on-delete" default="noaction" type="OnDeleteEnum"/>
</xs:extension> </xs:extension>
</xs:complexContent> </xs:complexContent>
</xs:complexType> </xs:complexType>
@ -1021,6 +1022,7 @@
<xs:attribute name="unique" default="false" type="xs:boolean"/> <xs:attribute name="unique" default="false" type="xs:boolean"/>
<xs:attribute name="unique-key" type="xs:string"/> <xs:attribute name="unique-key" type="xs:string"/>
<xs:attribute name="update" default="true" type="xs:boolean"/> <xs:attribute name="update" default="true" type="xs:boolean"/>
<xs:attribute name="on-delete" default="noaction" type="OnDeleteEnum"/>
</xs:extension> </xs:extension>
</xs:complexContent> </xs:complexContent>
</xs:complexType> </xs:complexType>

View File

@ -0,0 +1,104 @@
package org.hibernate.test.ondelete.toone;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import org.hibernate.Session;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Vlad Mihalcea
*/
public class ToOneOnDeleteTest extends BaseNonConfigCoreFunctionalTestCase {
@Test
public void testManyToOne() throws Exception {
Session session = openSession();
session.getTransaction().begin();
Parent parent = new Parent();
parent.id = 1L;
session.persist( parent );
Child child1 = new Child();
child1.id = 1L;
child1.parent = parent;
session.persist( child1 );
GrandChild grandChild11 = new GrandChild();
grandChild11.id = 1L;
grandChild11.parent = child1;
session.persist( grandChild11 );
Child child2 = new Child();
child2.id = 2L;
child2.parent = parent;
session.persist( child2 );
GrandChild grandChild21 = new GrandChild();
grandChild21.id = 2L;
grandChild21.parent = child2;
session.persist( grandChild21 );
GrandChild grandChild22 = new GrandChild();
grandChild22.id = 3L;
grandChild22.parent = child2;
session.persist( grandChild22 );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
parent = session.get( Parent.class, 1L );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {
Parent.class,
Child.class,
GrandChild.class
};
}
@Entity(name = "Parent")
public static class Parent {
@Id
private Long id;
}
@Entity(name = "Child")
public static class Child {
@Id
private Long id;
@ManyToOne
@OnDelete(action = OnDeleteAction.CASCADE)
private Parent parent;
}
@Entity(name = "GrandChild")
public static class GrandChild {
@Id
private Long id;
@OneToOne
@OnDelete(action = OnDeleteAction.CASCADE)
private Child parent;
}
}

View File

@ -0,0 +1,33 @@
/*
* 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.ondelete.toone.hbm;
/**
* @author Vlad Mihalcea
*/
public class Child {
private Long 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,33 @@
/*
* 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.ondelete.toone.hbm;
/**
* @author Vlad Mihalcea
*/
public class GrandChild {
private Long id;
private Child parent;
public Child getParent() {
return parent;
}
public void setParent(Child parent) {
this.parent = parent;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@ -0,0 +1,23 @@
/*
* 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.ondelete.toone.hbm;
/**
* @author Vlad Mihalcea
*/
public class Parent {
private Long id;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!--
~ 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>.
-->
<hibernate-mapping package="org.hibernate.test.ondelete.toone.hbm">
<class name="Parent">
<id name="id" type="long">
<generator class="assigned"/>
</id>
</class>
<class name="Child">
<id name="id" type="long">
<generator class="assigned"/>
</id>
<many-to-one name="parent" class="Parent" on-delete="cascade"/>
</class>
<class name="GrandChild">
<id name="id" type="long">
<generator class="assigned"/>
</id>
<many-to-one name="parent" class="Child" unique="true" not-null="true"
on-delete="cascade"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,66 @@
/*
* 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.ondelete.toone.hbm;
import org.hibernate.Session;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
/**
* @author Vlad Mihalcea
*/
public class ToOneOnDeleteHbmTest extends BaseCoreFunctionalTestCase {
@Test
public void testManyToOne() throws Exception {
Session session = openSession();
session.getTransaction().begin();
Parent parent = new Parent();
parent.setId( 1L );
session.persist( parent );
Child child1 = new Child();
child1.setId( 1L );
child1.setParent( parent );
session.persist( child1 );
GrandChild grandChild11 = new GrandChild();
grandChild11.setId( 1L );
grandChild11.setParent( child1 );
session.persist( grandChild11 );
Child child2 = new Child();
child2.setId( 2L );
child2.setParent( parent );
session.persist( child2 );
GrandChild grandChild21 = new GrandChild();
grandChild21.setId( 2L );
grandChild21.setParent( child2 );
session.persist( grandChild21 );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
parent = session.get( Parent.class, 1L );
session.delete( parent );
session.getTransaction().commit();
session.close();
}
@Override
public String[] getMappings() {
return new String[] { "ondelete/toone/hbm/ToOneOnDelete.hbm.xml" };
}
}