Hibernate version:
2.1
Mapping documents:
<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"> <key column="promotionId"/> <index column="position"/> <one-to-many class="ncr.dpc.domain.condition.Condition"/> </list>
</class>
Code between sessionFactory.openSession() and session.close():
Session session = getSession();
session.setFlushMode(FlushMode.COMMIT);
Transaction transaction = null ;
boolean saved = (promotion.getId()==null)?(true):(false);
try {
transaction = session.beginTransaction();
// Save or update promotion
session.saveOrUpdate(promotion);
// If the promotion was saved
if (saved) {
// Update code
promotion.setCode(promotion.getId());
session.update(promotion);
}
// Commit
transaction.commit();
} catch (HibernateException e) {
rollbackTransaction(transaction);
// TODO Log
throw new NCRHomeException(e);
} finally {
closeSession(session);
}
Name and version of the database you are using:
MySql 4.1.8
The generated SQL (show_sql=true):
[3/1/05 17:31:20:120 ART] 450c450c SystemOut O Hibernate: insert into RECORDS (itemElegibility, promotionVariable) values (?, ?)
[3/1/05 17:31:20:126 ART] 450c450c SystemOut O Hibernate: insert into CONDITIONS (testValue, frecuentCustomerExclusion, rewardGrant, endTime, startTime, days, hostValue, bits, ConditionTypeId, condition_id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
When I try to commit the transaction, an exception is raised. That's not the problem. The problem is that when the line session.saveOrUpdate() is executed, the new objects added to the conditions collection is inserted in the db. The point is that an insert is executed even when the transaction is rolled back and it was not commited.
The code calls 3 methods to get a session, rollback a transaction and to close a session. They call the respective methods in the session and transaction objects.
Thanks, martin
|