HibernateCallback 中如何处理事务
悬赏:10 发布时间:2008-07-23 提问人:asjd23 (初级程序员)
public int deleteAll(final String id){
int re=0;
try{
Object i=this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int i=session.createQuery("update A set state='已删除' where id='"+id+"'").executeUpdate();
int j=session.createQuery("update B set state='已删除' where id='"+id+"'").executeUpdate();
int k=session.createQuery("update C set state='已删除' where id='"+id+"'").executeUpdate();
return null;
}
}
);
}catch(Exception e){
re=0;
}
return re;
}
如上程序,,,如何把里面的三个update语句处在同一事务中 ???
急............
int re=0;
try{
Object i=this.getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
int i=session.createQuery("update A set state='已删除' where id='"+id+"'").executeUpdate();
int j=session.createQuery("update B set state='已删除' where id='"+id+"'").executeUpdate();
int k=session.createQuery("update C set state='已删除' where id='"+id+"'").executeUpdate();
return null;
}
}
);
}catch(Exception e){
re=0;
}
return re;
}
如上程序,,,如何把里面的三个update语句处在同一事务中 ???
急............
采纳的答案
2008-07-23 lggege (资深程序员)
你要这样的?
Transaction tx = null;
try
{
tx = session.beginTransaction();
int i=session.createQuery("update A set state='已删除' where id='"+id+"'").executeUpdate();
int j=session.createQuery("update B set state='已删除' where id='"+id+"'").executeUpdate();
int k=session.createQuery("update C set state='已删除' where id='"+id+"'").executeUpdate();
tx.commit();
}
catch (HibernateException he)
{
tx.rollback();
throw he;
}
finally
{
}提问者对于答案的评价:
多谢了 !分不多,一点意思!
额外加分:15
其他回答
或者这样的在Spring里配置的?
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true" lazy-init="default" autowire="default" dependency-check="default">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="set*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" lazy-init="default" autowire="default" dependency-check="default">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="yourDao" parent="baseTransactionProxy">
<property name="target">
<bean class="com.your.YourDAO"/>
</property>
</bean>
llade (资深程序员) 2008-07-23




