Using hibernate 3.0.5.
I have setup Page (master) with a one-to-one with Lock (slave). Page.getLock() is cascade "all-delete-orphan", and Lock.getPage() is constrained:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-access="field">
<class name="uk.ac.warwick.sbr.AbstractPageImpl"
proxy="uk.ac.warwick.sbr.Page"
table="PAGE"
dynamic-insert="true"
dynamic-update="true">
<id name="id"
column="id"
type="java.lang.String"
unsaved-value="null">
<generator class="org.hibernate.id.UUIDHexGenerator"/>
</id>
<one-to-one name="lock"
cascade="all-delete-orphan"
class="uk.ac.warwick.sbr.LockImpl"/>
</class>
<!-- Lock is only used as part of Page. -->
<class name="uk.ac.warwick.sbr.LockImpl"
proxy="uk.ac.warwick.sbr.Lock"
lazy="true"
table="PAGE_LOCK">
<id name="id"
column="id"
type="java.lang.String">
<generator class="foreign">
<param name="property">page</param>
</generator>
</id>
<one-to-one name="page" class="uk.ac.warwick.sbr.AbstractPageImpl" constrained="true"/>
<property name="lockedBy" column="LOCKED_BY"/>
<property name="lockedSince" column="LOCKED_TIME"/>
</class>
</hibernate-mapping>
I can successfully save a lock by saving the page, and if I call page.setLock(null), then the association is indeed broken. The problem is that the row in the database still exists:
Code:
page.setLock(null);
((LockImpl)lock).setPage(null);
anotherSession.flush();
page = (HtmlPage) anotherSession.load(HtmlPageImpl.class, page.getId());
lock = page.getLock();
assertNull("lock still attached to page", lock);
List allLocks = anotherSession.createSQLQuery(
"select {lock.*} from page_lock lock"
)
.addEntity("lock", LockImpl.class)
.list();
assertTrue("orphan row exists", allLocks.isEmpty());
}
Ta.
Col