spring2.5 hibernate3.2 事务

上传的文件如下: jdbc.rar

(1)@Repository
@Transactional
public class HibernateClinic implements Clinic {

 @Autowired
 private SessionFactory sessionFactory;


 @Transactional(readOnly = true)
 @SuppressWarnings("unchecked")
 public Collection<Vet> getVets() {      //查询数据
  return sessionFactory.getCurrentSession().createQuery("from Vet vet order by vet.lastName, vet.firstName").list();
 }

public void storeOwner(Owner owner) {   //保存数据
  // Note: Hibernate3's merge operation does not reassociate the object
  // with the current Hibernate Session. Instead, it will always copy the
  // state over to a registered representation of the entity. In case of a
  // new entity, it will register a copy as well, but will not update the
  // id of the passed-in object. To still update the ids of the original
  // objects too, we need to register Spring's
  // IdTransferringMergeEventListener on our SessionFactory.
  sessionFactory.getCurrentSession().merge(owner);
 }

(2)

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
   p:dataSource-ref="dataSource" p:mappingResources="petclinic.hbm.xml">
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
    <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
   </props>
  </property>
  <property name="eventListeners">
   <map>
    <entry key="merge">
     <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
    </entry>
   </map>
  </property>
 </bean>

 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
   p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}"
   p:password="${jdbc.password}"/>

<context:property-placeholder location="classpath:jdbc.properties"/>  //加载jdbc.properties文件

<context:annotation-config/>   //annotation

<tx:annotation-driven/>           //transaction  annotation

 

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
   p:sessionFactory-ref="sessionFactory"/>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值