Hi.
Our application follows session-per-request-with-detached object pattern for application transactions and we have a problem with the re-association of the detached object.
That is, during the first request, the user adds a new element (BusinessHours) to the parent object (Hotline), which then becomes detached. After a new child element is added, a businessHoursSet collection whithin the hotline becomes dirty, therefore we have to use session.update(...) to reassociate the hotline with the new session in a second request.
However, during this update, elements of the businessHoursSet are not updated but saveOrUpdated. Consequently, all new elements of businessHoursSet are saved, which is not desired.
My question is: how do I correctly reassociate detached objects having dirty collections with a new session?
Why does update cascades to saveOrUpdate in child collections? I mean specifically the following code in SessionImpl:
Code:
private void doUpdate(Object object, Serializable id, ClassPersister persister) throws HibernateException {
if ( !persister.isMutable() ) {
log.trace("immutable instance passed to doUpdate(), locking");
reassociate(object, id, persister);
}
else {
doUpdateMutable(object, id, persister);
}
cascading++;
try {
Cascades.cascade(this, persister, object, Cascades.ACTION_SAVE_UPDATE, Cascades.CASCADE_ON_UPDATE); // do cascade
}
finally {
cascading--;
}
}
May be I miss something, but should it not be something like Cascades.ACTION_UPDATE instead of SAVE_UPDATE?
Your help is greately appreciated.
Hotline might be assumed as an object with id (long, -1 means new object) and businessHoursSet (a set of BusinessHours objects).
BusinessHours imight be assumed to be a simple object with long id (-1 means new).
Mapping is something like:
Code:
<class name="de.disy.vcc.bo.hotline.Hotline" table="hotlines">
<!-- <cache usage="read-write"/> -->
<id name="id" column="id" type="long" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">hotline_ids</param>
</generator>
</id>
....
<set name="businessHoursSet" table="hotline_business_hours_relations" cascade="all" lazy="true">
<key column="hotline_id" />
<many-to-many column="business_hours_id" class="de.disy.vcc.bo.hotline.BusinessHours"/>
</set>
...
</class>
<class name="de.disy.vcc.bo.hotline.BusinessHours" table="business_hours">
<id name="id" column="id" type="long" unsaved-value="-1">
<generator class="sequence">
<param name="sequence">business_hours_ids</param>
</generator>
</id>
<property name="day">
<column name="day" sql-type="smallint" not-null="true"/>
</property>
<property name="timeFrom">
<column name="time_from" sql-type="varchar(10)" not-null="true"/>
</property>
<property name="timeTo">
<column name="time_to" sql-type="varchar(10)" not-null="true"/>
</property>
</class>