2013-07-25 2 views
18

इस पोस्ट जब मैं निम्नलिखित निष्पादित मैं अपवाद निम्न हो रही है, मैं यह कैसे हल कर सकते हैं JPA How to get the value from database after persistसाझा EntityManager पर लेन-देन को बनाने के लिए अनुमति नहीं है - का उपयोग वसंत लेनदेन या EJB CMT

की निरंतरता में है?

Not allowed to create transaction on shared EntityManager - use Spring 
transactions or EJB CMT 

DAOImpl कोड

public void create(Project project) { 
     entityManager.persist(project); 
     entityManager.getTransaction().commit(); 
     project = entityManager.find(Project.class, project.getProjectId()); 
     entityManager.refresh(project); 
     System.out.println("Id -- " + project.getProjectId()); 
      System.out.println("no -- " + project.getProjectNo()); 
    } 

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <bean id="DataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="username" value="scott" /> 
     <property name="password" value="tiger" /> 
     <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> 
     <property name="url" value="jdbc:oracle:thin:@myserver:1521:ORCL" /> 
    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="DataSource" /> 
     <property name="packagesToScan" value="test.entity" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> 
       <property name="generateDdl" value="false" /> 
       <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" /> 
      </bean> 
     </property> 
    </bean> 

    <context:component-scan base-package="test.net" /> 

    <tx:annotation-driven transaction-manager="transactionManager"/> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean>   

    <context:annotation-config/> 

</beans> 

उत्तर

25

मुझे लगता है कि समस्या यहाँ है कि यद्यपि आप लेन-देन प्रबंधक के लिए सेम को परिभाषित किया है, तो आप हेवन @Transactional के साथ बनाने() विधि को एनोटेट नहीं किया गया है जो वसंत लेनदेन को सक्षम बनाता है रों।

entityManager.getTransaction().commit(); कथन भी हटाएं क्योंकि अब सभी लेनदेन प्रबंधन वसंत द्वारा संभाले जाएंगे, यदि आप कथन छोड़ देते हैं तो आपको फिर से वही त्रुटि मिल जाएगी।

संबंधित मुद्दे