<bean id="sf" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- ... --> </bean> <bean id="txManagerHibernate" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <!-- ... --> </bean>
※其實就是將Hibernate3改成Hibernate4而已
※如果transactionManager使用JDBC的org.springframework.jdbc.datasource.DataSourceTransactionManager,Hibernate3.x整合Spring3.x可以使用getCurrentSession,而Hibernate4.2之前整合Spring3.x會出「No Session found for current thread」的錯,網路上說加什麼thread,我試沒有用,而JTA的我沒試過
雖然只是改成4,我還是將這次的pom檔列出來好了
pom.xml
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.2.13.RELEASE</version> </dependency> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.2.21.Final</version> </dependency>
※applicationContext.xml和hibernate.cfg.xml都有的設定
applicationContext.xml
<context:annotation-config /> <context:component-scan base-package="dao.impl" /> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean> <bean id="sf" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="ds" /> <property name="configLocations" value="classpath:hibernate.cfg.xml" /> </bean> <tx:annotation-driven transaction-manager="txManagerHibernate" /> <bean id="txManagerHibernate" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sf" /> </bean> <tx:advice id="xxx" transaction-manager="txManagerHibernate"> <tx:attributes> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="edit*" propagation="REQUIRED" /> <tx:method name="save*" propagation="REQUIRED" /> <tx:method name="add*" propagation="REQUIRED" /> <tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="ooo" expression="execution(* dao.impl*.*(..))" /> <aop:advisor advice-ref="xxx" pointcut-ref="ooo" /> </aop:config>
※如果還想針對特定的方法才執行transaction就要加tx:advice和aop:config,一般都是針對service,而我自己測試用,懶的寫service,所以直接跳到dao,可參考官網
pom.xml
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency>
※加這個是因為aop會用到,而我沒有,只好下載了,spring3.x整合hibernate3.x和4.2之前的我都試過,都是OK的
hibernate.cfg.xml
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.use_sql_comments">true</property> <mapping resource="hbm/Dept.hbm.xml" /> </session-factory> </hibernate-configuration>
※hbm.xml原本是寫在LocalSessionFactoryBean裡面的mappingResources屬性,也可寫在這裡,兩者選其一即可
沒有留言:
張貼留言