jbarbede
Mar 11th, 2008, 05:56 PM
I've a system that use a plain Hibernate 3 API (like found in Spring documentation: Implementing DAOs based on plain Hibernate 3 API (http://static.springframework.org/spring/docs/2.5.x/reference/orm.html#orm-hibernate)) to allow Spring to access to my data.
To manage transactions between Spring and Hibernate, I use declarative transaction management with transaction advice and AOP (Spring documentation A first example (http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html#transaction-declarative)).
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerD ataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/project"/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>com/project/model/member/Member.hbm.xml</value>
<value>com/project/model/city/City.hbm.xml</value>
<value>com/project/model/meteostate/MeteoState.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="myTransactionManager">
<tx:attributes>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.livemeteo.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<!-- Member Service object-->
<bean id="myMemberService" class="com.project.service.member.MemberServiceImpl">
<property name="memberDao" ref="myMemberDao"/>
</bean>
<!-- Member DAO object -->
<bean id="myMemberDao" class="com.project.persistence.member.MemberDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- City Service object-->
<bean id="myCityService" class="com.project.service.city.CityServiceImpl">
<property name="cityDao" ref="myCityDao"/>
</bean>
<!-- City DAO object -->
<bean id="myCityDao" class="com.project.persistence.city.CityDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- MeteoState Service object-->
<bean id="myMeteoStateService" class="com.project.service.meteostate.MeteoStateServiceIm pl">
<property name="meteoStateDao" ref="myMeteoStateDao"/>
</bean>
<!-- MeteoState DAO object -->
<bean id="myMeteoStateDao" class="com.project.persistence.meteostate.MeteoStateDaoIm pl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>
I think this configuration is correct but I've LazyInitialisationException when I make tests. So if I've well understood, I've problems with the lazy loading of Hibernate.
But I don't see what is missing in my configuration. The transaction advice doesn't aim to solve this problem?
To manage transactions between Spring and Hibernate, I use declarative transaction management with transaction advice and AOP (Spring documentation A first example (http://static.springframework.org/spring/docs/2.5.x/reference/transaction.html#transaction-declarative)).
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerD ataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/project"/>
<property name="username" value="..."/>
<property name="password" value="..."/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFac toryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>com/project/model/member/Member.hbm.xml</value>
<value>com/project/model/city/City.hbm.xml</value>
<value>com/project/model/meteostate/MeteoState.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransa ctionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="myTransactionManager">
<tx:attributes>
<!-- other methods use the default transaction settings (see below) -->
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.livemeteo.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"/>
</aop:config>
<!-- Member Service object-->
<bean id="myMemberService" class="com.project.service.member.MemberServiceImpl">
<property name="memberDao" ref="myMemberDao"/>
</bean>
<!-- Member DAO object -->
<bean id="myMemberDao" class="com.project.persistence.member.MemberDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- City Service object-->
<bean id="myCityService" class="com.project.service.city.CityServiceImpl">
<property name="cityDao" ref="myCityDao"/>
</bean>
<!-- City DAO object -->
<bean id="myCityDao" class="com.project.persistence.city.CityDaoImpl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<!-- MeteoState Service object-->
<bean id="myMeteoStateService" class="com.project.service.meteostate.MeteoStateServiceIm pl">
<property name="meteoStateDao" ref="myMeteoStateDao"/>
</bean>
<!-- MeteoState DAO object -->
<bean id="myMeteoStateDao" class="com.project.persistence.meteostate.MeteoStateDaoIm pl">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
</beans>
I think this configuration is correct but I've LazyInitialisationException when I make tests. So if I've well understood, I've problems with the lazy loading of Hibernate.
But I don't see what is missing in my configuration. The transaction advice doesn't aim to solve this problem?