HHH-4726 - Add support for delete-orphan cascading to <one-to-one/>

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18560 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-01-15 05:38:57 +00:00
parent d23a4a4fd3
commit 56f6941260
10 changed files with 557 additions and 7 deletions

View File

@ -1615,13 +1615,9 @@ public final class HbmBinder {
Attribute fkNode = node.attribute( "foreign-key" );
if ( fkNode != null ) manyToOne.setForeignKeyName( fkNode.getValue() );
validateCascade( node, path );
}
private static void validateCascade(Element node, String path) {
String cascade = node.attributeValue( "cascade" );
if ( cascade != null && cascade.indexOf( "delete-orphan" ) >= 0 ) {
throw new MappingException( "single-valued associations do not support orphan delete: " + path );
throw new MappingException( "many-to-one attributes do not support orphan delete: " + path );
}
}
@ -1687,8 +1683,6 @@ public final class HbmBinder {
oneToOne.setPropertyName( node.attributeValue( "name" ) );
oneToOne.setReferencedEntityName( getEntityName( node, mappings ) );
validateCascade( node, path );
}
public static void bindOneToMany(Element node, OneToMany oneToMany, Mappings mappings)

View File

@ -109,6 +109,10 @@ public final class Cascade {
this.action = action;
}
private SessionFactoryImplementor getFactory() {
return eventSource.getFactory();
}
/**
* Cascade an action from the parent entity instance to all its children.
*
@ -206,6 +210,54 @@ public final class Cascade {
cascadeComponent( parent, child, (AbstractComponentType) type, anything );
}
}
else {
// potentially we need to handle orphan deletes for one-to-ones here...
if ( isLogicalOneToOne( type ) ) {
// We have a physical or logical one-to-one and from previous checks we know we
// have a null value. See if the attribute cascade settings and action-type require
// orphan checking
if ( style.hasOrphanDelete() && action.deleteOrphans() ) {
// value is orphaned if loaded state for this property shows not null
// because it is currently null.
final EntityEntry entry = eventSource.getPersistenceContext().getEntry( parent );
if ( entry != null ) {
final EntityType entityType = (EntityType) type;
final Object loadedValue = entry.getLoadedValue( entityType.getPropertyName() );
if ( loadedValue != null ) {
final String entityName = entityType.getAssociatedEntityName();
if ( log.isTraceEnabled() ) {
log.trace( "deleting orphaned entity instance: " + entityName );
}
eventSource.delete( entityName, loadedValue, false, new HashSet() );
}
}
}
}
}
}
/**
* Check if the association is a one to one in the logical model (either a shared-pk
* or unique fk).
*
* @param type The type representing the attribute metadata
*
* @return True if the attribute represents a logical one to one association
*/
private boolean isLogicalOneToOne(Type type) {
if ( ! type.isEntityType() ) {
return false;
}
final EntityType entityType = (EntityType) type;
if ( entityType.isOneToOne() ) {
// physical one-to-one
return true;
}
// todo : still need to handle the many-to-one w/ property-ref
// actually there is a question about whether the constrained side
// can declare the orphan-delete. If not, then the side declaring
// the orphan-delete can only ever be a <one-to-one/>
return false;
}
private boolean cascadeAssociationNow(AssociationType associationType) {

View File

@ -0,0 +1,92 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.junit.functional.FunctionalTestCase;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends FunctionalTestCase {
public DeleteOneToOneOrphansTest(String string) {
super( string );
}
public String[] getMappings() {
return new String[] { "orphan/one2one/fk/bidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -0,0 +1,50 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class Employee {
private Long id;
private EmployeeInfo info;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public EmployeeInfo getInfo() {
return info;
}
public void setInfo(EmployeeInfo info) {
this.info = info;
}
}

View File

@ -0,0 +1,57 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class EmployeeInfo {
private Long id;
private Employee employee;
public EmployeeInfo() {
}
public EmployeeInfo(Employee employee) {
this.employee = employee;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2010, 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
-->
<!DOCTYPE hibernate-mapping PUBLIC
'-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.fk.bidirectional" >
<class name="Employee">
<id name="id" type="long" column="id">
<generator class="increment" />
</id>
<one-to-one name="info"
property-ref="employee"
class="EmployeeInfo"
cascade="all,delete-orphan"
constrained="false" />
</class>
<class name="EmployeeInfo">
<id name="id" type="long" column="id">
<generator class="increment" />
</id>
<many-to-one name="employee"
column="employee_id"
unique="true"
not-null="true" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,92 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.pk.bidirectional;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.junit.functional.FunctionalTestCase;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class DeleteOneToOneOrphansTest extends FunctionalTestCase {
public DeleteOneToOneOrphansTest(String string) {
super( string );
}
public String[] getMappings() {
return new String[] { "orphan/one2one/pk/bidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
emp.setInfo( new EmployeeInfo( emp ) );
session.save( emp );
session.getTransaction().commit();
session.close();
}
private void cleanupData() {
Session session = openSession();
session.beginTransaction();
session.createQuery( "delete EmployeeInfo" ).executeUpdate();
session.createQuery( "delete Employee" ).executeUpdate();
session.getTransaction().commit();
session.close();
}
public void testOrphanedWhileManaged() {
createData();
Session session = openSession();
session.beginTransaction();
List results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
Employee emp = ( Employee ) results.get( 0 );
assertNotNull( emp.getInfo() );
emp.setInfo( null );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 0, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
}

View File

@ -0,0 +1,50 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.pk.bidirectional;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class Employee {
private Long id;
private EmployeeInfo info;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public EmployeeInfo getInfo() {
return info;
}
public void setInfo(EmployeeInfo info) {
this.info = info;
}
}

View File

@ -0,0 +1,57 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.pk.bidirectional;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class EmployeeInfo {
private Long id;
private Employee employee;
public EmployeeInfo() {
}
public EmployeeInfo(Employee employee) {
this.employee = employee;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2010, 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
-->
<!DOCTYPE hibernate-mapping PUBLIC
'-//Hibernate/Hibernate Mapping DTD 3.0//EN'
'http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd'>
<hibernate-mapping package="org.hibernate.test.orphan.one2one.pk.bidirectional" >
<class name="Employee">
<id name="id" type="long" column="id">
<generator class="increment" />
</id>
<one-to-one name="info"
class="EmployeeInfo"
cascade="all,delete-orphan"
constrained="false" />
</class>
<class name="EmployeeInfo">
<id name="id" type="long" column="id">
<generator class="foreign">
<param name="property">employee</param>
</generator>
</id>
<one-to-one name="employee"
class="Employee"
constrained="true" />
</class>
</hibernate-mapping>