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

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18571 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-01-18 15:18:16 +00:00
parent f163e9fd94
commit 6cfc0c043d
5 changed files with 240 additions and 2 deletions

View File

@ -225,8 +225,7 @@ else if ( type.isComponentType() ) {
// 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;
if ( entry != null && entry.getStatus() != Status.SAVING ) {
final Object loadedValue;
if ( componentPathStack.isEmpty() ) {
// association defined on entity
@ -245,6 +244,7 @@ else if ( type.isComponentType() ) {
// set of SQL statements though since we do not know the
// orphaned value, something a delete with a subquery to
// match the owner.
// final EntityType entityType = (EntityType) type;
// final String propertyPath = composePropertyPath( entityType.getPropertyName() );
loadedValue = null;
}

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.unidirectional;
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/unidirectional/Mapping.hbm.xml" };
}
private void createData() {
Session session = openSession();
session.beginTransaction();
Employee emp = new Employee();
session.save( emp );
emp.setInfo( new EmployeeInfo( emp.getId() ) );
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.unidirectional;
/**
* 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,48 @@
/*
* 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.unidirectional;
/**
* TODO : javadoc
*
* @author Steve Ebersole
*/
public class EmployeeInfo {
private Long id;
public EmployeeInfo() {
}
public EmployeeInfo(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}

View File

@ -0,0 +1,48 @@
<?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.unidirectional" >
<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="assigned" />
</id>
</class>
</hibernate-mapping>