调用init方法失败;嵌套异常是org.hibernate.HibernateException:无法找到当前的JTA事务

时间:2013-05-16 06:56:00

标签: java spring hibernate maven

当我运行此代码时,它会给出错误:

forInvocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to locate current JTA transaction 

我的资源包中包含jta属性和所有配置文件。

代码:

package baseDao;

public interface BaseDao {

    public void create(Object obj);
    public void delete(Object obj);
    public void update(Object obj);
    public void get(Object obj);
}


package baseDao;
import javax.annotation.PostConstruct;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;


public abstract  class BaseDaoImpl implements BaseDao {

    @Autowired
    private  SessionFactory usermanagementSessionFactory;
    private  Session session;
    @PostConstruct
    public void init(){
          session = usermanagementSessionFactory.getCurrentSession();
    }
    /*-----------------To save an object--------------*/

    public void create(Object obj){
        session.save(obj);
    }

    /*-----------------To delete an object--------------*/


    public void delete(Object obj){
        session.delete(obj);
    }

    /*-----------------To update an object--------------*/

    public void update(Object obj){
        session.update(obj);
    }

    /*-----------------To find/Get an object--------------*/

    public void get(Object obj){

    }



    protected SessionFactory getUsermanagementSessionFactory() {
        return usermanagementSessionFactory;
    }


    protected void setUsermanagementSessionFactory(
            SessionFactory usermanagementSessionFactory) {
        this.usermanagementSessionFactory = usermanagementSessionFactory;
    }

}



package com.ecom.data.access.user;

import com.ecom.data.entities.user.Pincodes;

import baseDao.BaseDaoImpl;

public class PincodeDao extends BaseDaoImpl {

    public void save(){
        Pincodes p = new Pincodes();
        p.setPincode(11);
        p.setDescription(12);
        create(p);
    }
}

测试:

package com.ecom.data.access.user;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ecom.data.entities.user.Pincodes;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/applicationContext-usermanagement-dao.xml",
        "/applicationContext-usermanagement-dao-test.xml" })
public class PiccodeTest extends AbstractTransactionalJUnit4SpringContextTests {


    @BeforeClass
    public static void setup(){}


    @Test
    public void pincodeTest(){
        /*Pincodes p = new Pincodes();          //object of entity pincode
        p.setPincode(11);
        p.setDescription(10);*/
        PincodeDao pinDao = new PincodeDao();   // object of dao class to call the create method
        pinDao.save();
        /*pinDao.create(p);
        Assert.assertTrue(Boolean.TRUE);*/
    }

}

Spring配置:

<?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:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee 
        http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <util:properties id="usermanagementHibernateProperties" location="classpath:usermanagement-hibernate.properties" />

    <bean id="usermanagementSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="usermanagementDataSource" />
        <property name="configLocation" value="classpath:hibernate.cfg.usermanagement.xml" />
        <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        <property name="hibernateProperties" ref="usermanagementHibernateProperties" />
    </bean>

    <jee:jndi-lookup id="usermanagementDataSource" jndi-name="java:usermanagementDS" />

    <bean id="city" class="com.ecom.data.entities.user.City"/>
    <bean id="state" class="com.ecom.data.entities.user.State"/>
    <bean id="country" class="com.ecom.data.entities.user.Country"/>
    <bean id="pincodes" class="com.ecom.data.entities.user.Pincodes"/>
    <bean id="notification" class="com.ecom.data.entities.notification.Notifications"/>
    <bean id="notification_types" class="com.ecom.data.entities.notification.Notification_Types"/>
    <bean id="transactions" class="com.ecom.data.entities.transaction.Transactions"/>
    <bean id="address" class="com.ecom.data.entities.user.Address"/>
    <bean id="user_master" class="com.ecom.data.entities.user.User_Master"/>
    <bean id="notification_channels" class="com.ecom.data.entities.notification.Notification_Channels"/>
    <bean id="notification_time" class="com.ecom.data.entities.notification.Notification_Time"/>
    <bean id="prefilled_response" class="com.ecom.data.entities.product.Prefilled_Response"/>
    <bean id="payment_options" class="com.ecom.data.entities.transaction.Payment_Options"/>
    <bean id="catagory" class="com.ecom.data.entities.product.Catagory"/>
    <bean id="vendor" class="com.ecom.data.entities.product.Vendor"/>
    <bean id="requester" class="com.ecom.data.entities.product.Requester"/>
    <bean id="requirement_type" class="com.ecom.data.entities.product.Requirement_type"/>
    <bean id="discount_offer_type" class="com.ecom.data.entities.product.Discount_Offer_Type"/>
    <bean id="discount_offers" class="com.ecom.data.entities.product.Discount_Offers"/>
    <bean id="requirements" class="com.ecom.data.entities.product.Requirements"/>
    <bean id="product_catalog" class="com.ecom.data.entities.product.Product_Catalog"/>
    <bean id="product_catalog_vendor" class="com.ecom.data.entities.product.Product_Catalog_Vendor"/>
    <bean id="product_vendor_payment_option_location" class="com.ecom.data.entities.product.PRODUCT_VENDOR_PAYMENT_OPTION_LOCATION"/>

    <bean id="baseDaoImpl" abstract="true"  class="baseDao.BaseDaoImpl"> </bean>
    <bean id="pincodeDao" parent="baseDaoImpl" class="com.ecom.data.access.user.PincodeDao"> </bean>


</beans>

JTA属性:

com.atomikos.icatch.output_dir=./target
com.atomikos.icatch.log_base_dir=./target
com.atomikos.icatch.service=com.atomikos.icatch.standalone.UserTransactionServiceFactory
com.atomikos.icatch.tm_unique_name = com.atomikos.spring.jdbc.tm

com.atomikos.icatch.automatic_resource_registration=false 
com.atomikos.icatch.max_actives=50
com.atomikos.icatch.console_log_level=DEBUG

错误:

SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@8905bd] to prepare test instance [com.ecom.data.access.user.PiccodeTest@1d7fc31]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'pincodeDao': Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to locate current JTA transaction
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:399)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:96)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:44)
    at org.springframework.test.context.TestContext.buildApplicationContext(TestContext.java:198)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:233)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:126)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.HibernateException: Unable to locate current JTA transaction
    at org.hibernate.context.internal.JTASessionContext.currentSession(JTASessionContext.java:88)
    at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:90)
    at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:980)
    at baseDao.BaseDaoImpl.init(BaseDaoImpl.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:344)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:295)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:130)
    ... 31 more

1 个答案:

答案 0 :(得分:0)

当您使用Atomikos时,有必要将其配置为Spring JTA事务管理器。 以下是如何操作的教程:http://www.atomikos.com/Documentation/SpringIntegration#Configuring_Atomikos_as_the_Spri