I'm sorry, I can't tell you what you are doing wrong when you show no code. But todays your lucky day 'cos i realized I didnt have a nicely formatted test for this so I wrote one up. You can see what you're *meant* to be doing and work from there:
Code:
<hibernate-mapping package="org.hibernate.test.orphan">
<class name="Product">
<id name="name"/>
<set name="parts" cascade="all,delete-orphan" fetch="join">
<key column="productName" not-null="true"/>
<one-to-many class="Part"/>
</set>
</class>
<class name="Part">
<id name="name"/>
<property name="description" not-null="true"/>
</class>
</hibernate-mapping>
Code:
public class Product {
private String name;
private Set parts = new HashSet();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getParts() {
return parts;
}
public void setParts(Set parts) {
this.parts = parts;
}
}
Code:
public class Part {
private String name;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Code:
public class OrphanTest extends TestCase {
public OrphanTest(String str) {
super(str);
}
public void testOrphanDeleteOnSaveOrUpdate() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.create(prod);
t.commit();
session.close();
prod.getParts().remove(part);
session = openSession();
t = session.beginTransaction();
session.saveOrUpdate(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
public void testOrphanDelete() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.create(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
prod = (Product) session.get(Product.class, "Widget");
assertTrue( Hibernate.isInitialized( prod.getParts() ) );
part = (Part) session.get(Part.class, "Widge");
prod.getParts().remove(part);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
public void testOrphanDeleteOnMerge() {
Session session = openSession();
Transaction t = session.beginTransaction();
Product prod = new Product();
prod.setName("Widget");
Part part = new Part();
part.setName("Widge");
part.setDescription("part if a Widget");
prod.getParts().add(part);
Part part2 = new Part();
part2.setName("Get");
part2.setDescription("another part if a Widget");
prod.getParts().add(part2);
session.create(prod);
t.commit();
session.close();
prod.getParts().remove(part);
session = openSession();
t = session.beginTransaction();
session.merge(prod);
t.commit();
session.close();
session = openSession();
t = session.beginTransaction();
assertNull( session.get(Part.class, "Widge") );
assertNotNull( session.get(Part.class, "Get") );
session.delete( session.get(Product.class, "Widget") );
t.commit();
session.close();
}
protected String[] getMappings() {
return new String[] { "orphan/Product.hbm.xml" };
}
public static Test suite() {
return new TestSuite(OrphanTest.class);
}
}