【问题标题】:Cannot use an EntityTransaction while using JTA (No manual transaction statements)使用 JTA 时无法使用 EntityTransaction(无手动事务语句)
【发布时间】:2015-05-12 00:26:21
【问题描述】:

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

代码如下:

主道

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>

spring-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.

任何帮助将不胜感激。

【问题讨论】:

    标签: java glassfish entity derby


    【解决方案1】:

    像这样将 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">

    【讨论】:

      猜你喜欢
      • 2012-06-10
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 2014-07-01
      • 2017-05-07
      • 1970-01-01
      • 2020-02-16
      相关资源
      最近更新 更多