You might need this instant guide.
Transactions with Manager and Advice
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
Transactions with Pointcuts
<aop:config>
<aop:pointcut id="projectServiceOperation" expression="execution(* <package>.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceOperation" />
</aop:config>
Hibernate Session Factory
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value><classpath></value>
</list>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:hbm</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">false</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
</entry>
</map>
</property>
</bean>
Hibernate DAO and Service
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<bean id="(dao)" class="(daoClass)">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="(service)" class="(serviceClass)">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
Best Practice
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
</tx:attributes>
</tx:advice>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.connection.autocommit">false</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:hbm</value>
</list>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="entityInterceptor">
<ref local="hibernateInterceptor" />
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/foo</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="nestedTransactionAllowed">
<value>true</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="cacheQueries">
<value>true</value>
</property>
</bean>
<TBC>
本文介绍了如何在Spring框架中配置事务管理,并详细说明了Hibernate与Spring集成的方式,包括SessionFactory的设置、DAO及Service层的设计,以及最佳实践。
1007

被折叠的 条评论
为什么被折叠?



