使用JTA时无法使用EntityTransaction(无手动事务语句)

时间:2015-03-10 14:06:39

标签: java apache glassfish entity derby

我理解这个查询已在其他讨论中处理过,但我没有将@Transactional与em.getTranstaction.begin()或任何其他手动事务语句混合使用。 (使用glassfish和netbeans 8)

以下代码:

MainDao

package com.restaurant.orders;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class MainDao {
    
    @PersistenceContext private EntityManager em;
    
    @Transactional
    public void addUser(Users user) {
        em.persist(user);
    }

    @Transactional
    public void addOrder(Orders order) {
        em.persist(order);
    }
    
    @Transactional
    public void addOrderItem(Orderitems orderitem) {
        em.persist(orderitem);
    }
    
    public List<Users> getAllUsers() {
    TypedQuery<Users> query = em.createQuery(
        "SELECT g FROM Users g ORDER BY g.id", Users.class);
    return query.getResultList();
    }
    
    public List<Orders> getAllOrders() {
    TypedQuery<Orders> query = em.createQuery(
        "SELECT g FROM Orders g ORDER BY g.id", Orders.class);
    return query.getResultList();
    }
    
    public List<Orderitems> getAllOrderItems() {
    TypedQuery<Orderitems> query = em.createQuery(
        "SELECT g FROM Orderitems g ORDER BY g.id", Orderitems.class);
    return query.getResultList();
    }
    
}

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="com.restaurant_restaurant_war_1.0-SNAPSHOTPU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>orders</jta-data-source>
    <class>com.restaurant.orders.Users</class>
    <class>com.restaurant.orders.Orderitems</class>
    <class>com.restaurant.orders.Orders</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

弹簧servlet.xml中

<?xml version="1.0" encoding="windows-1252"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
 
  <!-- Use @Component annotations for bean definitions -->
  <context:component-scan base-package="com.restaurant.orders"/>
 
  <!-- Use @Controller annotations for MVC controller definitions -->
  <mvc:annotation-driven />
 

  <!-- Add JPA support -->
  <bean id="emf" class=
       "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
     <property name="loadTimeWeaver">
        <bean class=
 "org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
      </property>
  </bean>
 
  <!-- Add Transaction support -->
  <bean id="myTxManager"
     class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="emf"/>
  </bean>
 
  <!-- Use @Transaction annotations for managing transactions -->
  <tx:annotation-driven transaction-manager="myTxManager" />
 
  <!-- View resolver -->
 <bean class=
     "org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/WEB-INF/"/>
 </bean>
 
</beans>

跟踪:

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
root cause

org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
root cause

java.lang.IllegalStateException: 
Exception Description: Cannot use an EntityTransaction while using JTA.
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs.

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

将metadata-complete =“true”添加到您的web.xml中

<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         metadata-complete="true">