Hibernate version: 2.1
Mapping documents:
Parent class:
<class name="ncr.dpc.domain.Promotion" table="PROMOTIONS">
<id name="id" column="promotionId" type="integer" unsaved-value="null">
<generator class="native"/>
</id>
<property name="code" type="integer"/>
<property name="customerEmployeeDiac" type="boolean"/>
<property name="customerNotEmployeeDiac" type="boolean"/>
<property name="customerRecordDiac" type="boolean"/>
<property name="description" type="string"/>
<property name="directed" type="boolean"/>
<property name="instantWin" type="boolean"/>
<property name="instantWinEvaluationMode" type="integer"/>
<property name="instantWinPrizeOddsOfWinning" type="integer"/>
<property name="instantWinPrizeQuantity" type="integer"/>
<property name="instantWinOddsCalculation" type="integer"/>
<property name="endDate" type="date"/>
<property name="endTime" type="string"/>
<property name="offlineDiac" type="integer"/>
<property name="priority" type="integer"/>
<property name="rewardLimitation" type="integer"/>
<property name="rewardLimitationTimes" type="integer"/>
<property name="rewardLimitationTransaction" type="integer"/>
<property name="startDate" type="date"/>
<property name="startTime" type="string"/>
<property name="state" type="integer"/>
<many-to-one name="promotionVariable" column="promotionVariableId" class="ncr.dpc.domain.PromotionVariable"/>
<many-to-one name="originalPromotion" class="ncr.dpc.domain.Promotion" column="originalPromotionId" unique="true"/>
<many-to-one name="category" class="ncr.dpc.domain.PromotionCategory" column="promotionCategoryId"/>
<list name="conditions" cascade="all-delete-orphan" lazy="true" access="field">
<key column="promotionId"/>
<index column="position"/>
<one-to-many class="ncr.dpc.domain.condition.Condition"/>
</list>
</class>
Code between sessionFactory.openSession() and session.close():
This is the method in the persistence layer to initialize a lazy collection
Code:
public List retrieveConditions(Promotion promotion) throws NCRHomeException {
Session session = getSession();
session.setFlushMode(FlushMode.NEVER);
Transaction tr;
try {
tr = session.beginTransaction();
session.update(promotion);
if (!Hibernate.isInitialized(promotion.conditions)) {
Hibernate.initialize(promotion.conditions);
}
tr.rollback();
return promotion.conditions;
} catch (HibernateException e) {
//@TODO Log
throw new HomeException(e);
} finally {
this.closeSession(session);
}
}
Name and version of the database you are using:
MySql 4.1.8
The case is that a need to work with the objects over several transactions, only saving it when the user presses the save button. The problem arises when a add a new object to the lazy collection. For some reason, the session.updates()
does update the lazy collection, inserting in the db the new objects. But when a remove the cascade attribute, this doesn't happend. It's very useful using the cascade attribute, so I'd like to know if I'm doing something wrong.
Thanks,
Martin.[/quote]