HHH-2146 : NullpointerException in DefaultDeleteEventListener.deleteTransientEntity

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@15445 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2008-10-29 21:33:52 +00:00
parent 4a058ff6ca
commit bfb989a467
6 changed files with 183 additions and 2 deletions

View File

@ -25,6 +25,7 @@
package org.hibernate.engine;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import org.slf4j.Logger;
@ -375,7 +376,7 @@ public final class Cascade {
if ( log.isTraceEnabled() ) {
log.trace("deleting orphaned entity instance: " + entityName);
}
eventSource.delete( entityName, orphan, false, null );
eventSource.delete( entityName, orphan, false, new HashSet() );
}
}
}

View File

@ -1,5 +1,8 @@
package org.hibernate.test.deletetransient;
import java.util.Set;
import java.util.HashSet;
/**
* todo: describe Address
*
@ -8,6 +11,7 @@ package org.hibernate.test.deletetransient;
public class Address {
private Long id;
private String info;
private Set suites = new HashSet();
public Address() {
}
@ -31,4 +35,12 @@ public class Address {
public void setInfo(String info) {
this.info = info;
}
public Set getSuites() {
return suites;
}
public void setSuites(Set suites) {
this.suites = suites;
}
}

View File

@ -105,4 +105,70 @@ public class DeleteTransientEntityTest extends FunctionalTestCase {
t.commit();
s.close();
}
public void testCascadeAllFromClearedPersistentAssnToTransientEntity() {
Session s = openSession();
Transaction t = s.beginTransaction();
Person p = new Person();
Address address = new Address();
address.setInfo( "123 Main St." );
p.getAddresses().add( address );
s.save( p );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Suite suite = new Suite();
address.getSuites().add( suite );
p.getAddresses().clear();
s.saveOrUpdate( p );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
p = ( Person ) s.get( p.getClass(), p.getId() );
assertEquals( "persistent collection not cleared", 0, p.getAddresses().size() );
Long count = ( Long ) s.createQuery( "select count(*) from Address" ).list().get( 0 );
assertEquals( 1, count.longValue() );
count = ( Long ) s.createQuery( "select count(*) from Suite" ).list().get( 0 );
assertEquals( 0, count.longValue() );
s.delete( p );
t.commit();
s.close();
}
public void testCascadeAllDeleteOrphanFromClearedPersistentAssnToTransientEntity() {
Session s = openSession();
Transaction t = s.beginTransaction();
Address address = new Address();
address.setInfo( "123 Main St." );
Suite suite = new Suite();
address.getSuites().add( suite );
s.save( address );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Note note = new Note();
note.setDescription( "a description" );
suite.getNotes().add( note );
address.getSuites().clear();
s.saveOrUpdate( address );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Long count = ( Long ) s.createQuery( "select count(*) from Suite" ).list().get( 0 );
assertEquals( "all-delete-orphan not cascaded properly to cleared persistent collection entities", 0, count.longValue() );
count = ( Long ) s.createQuery( "select count(*) from Note" ).list().get( 0 );
assertEquals( 0, count.longValue() );
s.delete( address );
t.commit();
s.close();
}
}

View File

@ -0,0 +1,33 @@
package org.hibernate.test.deletetransient;
/**
*
* @author Gail Badner
*/
public class Note {
private Long id;
private String description;
public Note() {
}
public Note(String description) {
this.description = description;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -29,6 +29,28 @@
<generator class="increment"/>
</id>
<property name="info" type="string"/>
<set name="suites" lazy="true" inverse="false" cascade="all-delete-orphan">
<key column="ADDRESS_ID"/>
<one-to-many class="Suite"/>
</set>
</class>
<class name="Suite" table="T_SUITE">
<id name="id" type="long">
<generator class="increment"/>
</id>
<property name="location" type="string"/>
<set name="notes" lazy="true" inverse="false" cascade="all-delete-orphan">
<key column="SUITE_ID"/>
<one-to-many class="Note"/>
</set>
</class>
<class name="Note" table="T_NOTE">
<id name="id" type="long">
<generator class="increment"/>
</id>
<property name="description" type="string"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,47 @@
package org.hibernate.test.deletetransient;
import java.util.Set;
import java.util.HashSet;
import java.util.Collection;
import java.util.ArrayList;
/**
*
* @author Gail Badner
*/
public class Suite {
private Long id;
private String location;
private Set notes = new HashSet();
public Suite() {
}
public Suite(String location) {
this.location = location;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Set getNotes() {
return notes;
}
public void setNotes(Set notes) {
this.notes = notes;
}
}