[
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6484?page=c...
]
Mauricio Hiroshi Nagaoka commented on HHH-6484:
-----------------------------------------------
The code below shows the two test cases I added to DeleteOneToOneOrphansTest.java from the
hibernate-testsuite project.
The first one is pretty much the same as the already existing
{{testOrphanedWhileManaged()}} but instead of setting {{Employee.employeeInfo}} to
{{null}}, it is setting it to a new, unmanaged instance. It is expected that the previous
{{EmployeeInfo}} instance would be deleted from the database since it is part of a
one-to-one delete-orphan relationship.
{code:java}
public void testReplacedWhileManaged() {
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() );
// Replace with a new EmployeeInfo instance
emp.setInfo( new EmployeeInfo(emp) );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
{code}
The second one is similar, but forces a session flush after setting
{{Employee.employeeInfo}} to {{null}}. It causes Hibernate to correctly issue a SQL DELETE
and then, a SQL INSERT is issued due to the new {{EmployeeInfo}} instance associated. This
can be seen as a workaround, but it would require the domain/business logic objects to
deal with the Hibernate API directly.
{code:java}
public void testReplacedWhileManaged_FlushingBeforeReplacing() {
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() );
// Replace with a new EmployeeInfo instance
emp.setInfo( null );
session.flush();
emp.setInfo( new EmployeeInfo(emp) );
session.getTransaction().commit();
session.close();
session = openSession();
session.beginTransaction();
emp = ( Employee ) session.get( Employee.class, emp.getId() );
assertNotNull( emp.getInfo() );
results = session.createQuery( "from EmployeeInfo" ).list();
assertEquals( 1, results.size() );
results = session.createQuery( "from Employee" ).list();
assertEquals( 1, results.size() );
session.getTransaction().commit();
session.close();
cleanupData();
}
{code}
Replacing an entity on a one-to-one delete-orphan association with a
new instance does not delete the previous one
------------------------------------------------------------------------------------------------------------------
Key: HHH-6484
URL:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-6484
Project: Hibernate Core
Issue Type: Bug
Components: core
Affects Versions: 3.6.6
Reporter: Mauricio Hiroshi Nagaoka
Attachments: hibernate-testsuite-3.6.6.Final.patch
On {{DeleteOneToOneOrphansTest}}, setting {{Employee.employeeInfo}} to a new
{{EmployeeInfo}} instance (instead of setting it to {{null}}) doesn't trigger the
deletion of the old {{employeeInfo}} raising a {{ConstraintViolationException}}.
Please refer to the attached test suite patch.
This is somewhat related to HHH-4726.
--
This message is automatically generated by JIRA.
For more information on JIRA, see:
http://www.atlassian.com/software/jira