Ok - this is confusing now, I can extend the hibernate tests and it does what I want, but the JPA version does not...
Note now using hibernate-3.2.6.
I get this exception:
Quote:
No row with the given identifier exists: [org.hibernate.ejb.test.cascade.Soldier#1]
javax.persistence.EntityNotFoundException: No row with the given identifier exists: [org.hibernate.ejb.test.cascade.Soldier#1]
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.refresh(AbstractEntityManagerImpl.java:269)
at org.hibernate.ejb.test.cascade.FetchTest.testDeleteRefreshProblem(FetchTest.java:63)
Core hibernate test, added to DeleteTest class:
Code:
public void testRefreshAfterDeleteInOtherSession() {
Session s = openSession();
s.beginTransaction();
Node parent = new Node( "parent" );
Node child = new Node( "child" );
parent.getCascadingChildren().add( child );
assertEquals("s parent has 1 child", 1, parent.getCascadingChildren().size());
s.persist( parent );
s.getTransaction().commit();
deleteChildViaOtherSession("parent");
s.refresh(parent);
assertEquals("s parent has no children", 0, parent.getCascadingChildren().size());
}
private Node deleteChildViaOtherSession(String parentId) {
Node parent;
Session s2 = openSession();
s2.beginTransaction();
parent = ( Node ) s2.get( Node.class, parentId);
for (Iterator iterator = parent.getCascadingChildren().iterator(); iterator.hasNext();) {
Object o = iterator.next();
iterator.remove();
}
s2.getTransaction().commit();
s2.close();
return parent;
}
And its hbm file (unchanged from the distribution):
Code:
<class name="Node" polymorphism="explicit">
<id name="name">
<generator class="assigned"/>
</id>
<property name="description"/>
<many-to-one name="parent"/>
<property name="created" not-null="true"/>
<set name="children"
inverse="true"
cascade="persist,merge,save-update,evict">
<key column="parent"/>
<one-to-many class="Node"/>
</set>
<set name="cascadingChildren" inverse="false" cascade="persist,merge,save-update,evict,delete">
<key column="CASC_PARENT"/>
<one-to-many class="Node"/>
</set>
</class>
Here is the JPA version, added to the FetchTest:
Code:
public void testDeleteRefreshProblem() throws Exception {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Troop disney = new Troop();
disney.setName( "Disney" );
Soldier mickey = new Soldier();
mickey.setName( "Mickey" );
disney.addSoldier( mickey );
em.persist( disney );
em.getTransaction().commit();
removeMickey(disney.getId());
em.refresh(disney);
assertEquals("Check no soldiers", 0, disney.getSoldiers().size());
}
private void removeMickey(Integer id) {
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();
Troop disney = em.find( Troop.class, id );
disney.getSoldiers().clear();
em.getTransaction().commit();
em.close();
}