Results 1 to 4 of 4

Thread: Help needed with Hibernate and Spring

  1. #1

    Default Help needed with Hibernate and Spring

    Hello,

    I am trying to setup the Hibernate session and transaction management with spring 2.04. I am running into the error below. I have also pasted my application context.xml file.

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/commonContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' must be specified
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/commonContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' must be specified
    Caused by: java.lang.IllegalStateException: Either 'userTransaction' or 'userTransactionName' or 'transactionManager' or 'transactionManagerName' must be specified
    	at org.springframework.transaction.jta.JtaTransactionManager.initUserTransactionAndTransactionManager(JtaTransactionManager.java:458)
    	at org.springframework.transaction.jta.JtaTransactionManager.afterPropertiesSet(JtaTransactionManager.java:407)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1175)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1145)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:261)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:109)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1073)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:835)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:423)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    	at org.springframework.aop.support.AbstractBeanFactoryPointcutAdvisor.getAdvice(AbstractBeanFactoryPointcutAdvisor.java:77)
    	at org.springframework.aop.aspectj.AspectJProxyUtils.makeAdvisorChainAspectJCapableIfNecessary(AspectJProxyUtils.java:49)
    	at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.extendAdvisors(AspectJAwareAdvisorAutoProxyCreator.java:98)
    	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:90)
    	at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:69)
    	at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:296)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:314)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1154)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:427)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:251)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:144)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:248)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:279)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
    	at test.diamelle.common.service.ServiceMgrTest.onSetUp(ServiceMgrTest.java:36)
    	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:89)
    	at junit.framework.TestCase.runBare(TestCase.java:128)
    	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    	at junit.framework.TestResult$1.protect(TestResult.java:110)
    	at junit.framework.TestResult.runProtected(TestResult.java:128)
    	at junit.framework.TestResult.run(TestResult.java:113)
    	at junit.framework.TestCase.run(TestCase.java:120)
    	at junit.framework.TestSuite.runTest(TestSuite.java:228)
    	at junit.framework.TestSuite.run(TestSuite.java:223)
    	at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    ApplicationContext.xml:

    Code:
    	<!-- Service Beans -->
    	<bean id="serviceManager" class="diamelle.common.service.ServiceMgr" >
    		<constructor-arg ref="serviceDAO"></constructor-arg>
    	</bean>
    	
    	<bean id="serviceDAO" class="diamelle.common.service.ServiceDAOBean" >
    		<property name="sessionFactory" ref="sessionFactory"></property>		
    	</bean>
    
    
    	<!-- Hibernate SessionFactory -->
    	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    		<property name="dataSource" ref="dataSource"/>
    		<property name="mappingResources">
    			<list>
    				<value>diamelle/common/service/Service.hbm.xml</value>
    				<value>diamelle/common/service/ServiceConfig.hbm.xml</value>
    				<value>diamelle/common/service/RequestForm.hbm.xml</value>
    				<value>diamelle/common/service/RequestApprover.hbm.xml</value>
    			</list>
    		</property>
    	
    		<property name="hibernateProperties">
    			<props>
    				<!-- list of dialect avaialable at:
    				http://www.hibernate.org/hib_docs/v3/api/org/hibernate/dialect/package-summary.html
    				 -->
    				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				<prop key="hibernate.generate_statistics">true</prop>
    			</props>
    		</property>
    	</bean>
    
    
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
    		<property name="url" value="jdbc:oracle:thin:@172.17.2.121:1521:orcl"/>
    		<property name="username" value="idmuser"/>
    		<property name="password" value="idmuser"/>
    	</bean>
    
    
    	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    	</bean>
    
    
      <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- the transactional semantics... -->
        <tx:attributes>
          <!-- all methods starting with 'get' are read-only -->
          <tx:method name="get*" read-only="true" propagation="SUPPORTS" />
          <!-- other methods use the default transaction settings (see below) -->
          <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
      </tx:advice>
      
      <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(* diamelle.common.service.ServiceMgr.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
      </aop:config>
    Thanks in advance for your help.

  2. #2
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi

    You are specifying the JtaTransactionManager, but are declaring a local DataSource. Maybe this is what you want to do, but I would think it unlikely.

    Change the bean definition of the 'transactionManager' bean to HibernateTransactionManager, like so.

    Code:
      <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="mySessionFactory"/>
      </bean>
    See also this section of the Spring reference documentation.

    If you do require global transactions, then ping back this thread.

    Cheers
    Rick

  3. #3

    Default

    Hi Rick,

    thanks very much for your help. that solved the problem.
    However, I would like to use my appservers datasource and the jta to manage transactions. I am moving a bunch of ejbs over to spring/hibernate. I was wondering if you had an example that I could use for reference to achieve my above goals.

    thanks
    Suneet

  4. #4
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi Suneet

    (Taken from the reference manual.)

    If we use JTA in a J2EE container we use a container DataSource, obtained via JNDI, in conjunction with Spring's JtaTransactionManager. The JtaTransactionManager doesn't need to know about the DataSource, or any other specific resources, as it will use the container's global transaction management infrastructure.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
    
      <jee:jndi-lookup id="dataSource" jndi-name="jdbc/jpetstore"/> 
    
      <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager" />
      
      <!-- other <bean/> definitions here -->
    
    </beans>
    With Hibernate and JTA transactions, we can simply use the JtaTransactionManager as with JDBC or any other resource strategy.

    Code:
    <bean id="txManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
    Of course, using global transactions requires a lot more thought than this simple configuration. Do you have a really good reason for favouring global transactions over local transactions?

    Cheers
    Rick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •