在使用JTA(无需人工交易报表)不能使用EntityTransaction [英] Cannot use an EntityTransaction while using JTA (No manual transaction statements)

查看:305
本文介绍了在使用JTA(无需人工交易报表)不能使用EntityTransaction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白这个查询在其他讨论中得到处理,但我没有用em.getTranstaction.begin()或任何其它手动事务声明混合@Transactional。 (使用的是GlassFish和NetBeans 8)

code如下:

MainDao

\r
\r

包com.restaurant.orders;\r
\r
进口的java.util.List;\r
进口javax.persistence.EntityManager;\r
进口javax.persistence.PersistenceContext;\r
进口javax.persistence.TypedQuery;\r
进口org.springframework.stereotype.Component;\r
进口org.springframework.transaction.annotation.Transactional;\r
\r
@零件\r
公共类MainDao {\r
    \r
    @PersistenceContext EntityManager的私人EM;\r
    \r
    @Transactional\r
    公共无效ADDUSER(用户用户){\r
        em.persist(用户);\r
    }\r
\r
    @Transactional\r
    公共无效addOrder(订货下单){\r
        em.persist(订单);\r
    }\r
    \r
    @Transactional\r
    公共无效addOrderItem(的OrderItems OrderItem的){\r
        em.persist(OrderItem的);\r
    }\r
    \r
    公开名单<用户> getAllUsers(){\r
    TypedQuery<用户>查询= em.createQuery(\r
        选择G以用户的摹ORDER BY g.id,Users.class);\r
    返回query.getResultList();\r
    }\r
    \r
    公开名单<订单> getAllOrders(){\r
    TypedQuery<订单>查询= em.createQuery(\r
        选择G以从接单摹ORDER BY g.id,Orders.class);\r
    返回query.getResultList();\r
    }\r
    \r
    公开名单<&的OrderItems GT; getAllOrderItems(){\r
    TypedQuery<&的OrderItems GT;查询= em.createQuery(\r
        选择g在的OrderItems摹ORDER BY g.id,Orderitems.class);\r
    返回query.getResultList();\r
    }\r
    \r
}

\r

\r
\r

的persistence.xml

\r
\r

<?XML版本=1.0编码=UTF-8? >\r
<持久性版本=2.0的xmlns =htt​​p://java.sun.com/xml/ns/persistence的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instanceXSI:的schemaLocation =htt​​p://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd\">\r
  <持久性单位名称=com.restaurant_restaurant_war_1.0-SNAPSHOTPU交易型=JTA>\r
    <提供商GT; org.eclipse.persistence.jpa.PersistenceProvider< /提供商GT;\r
    < JTA数据源>订单< / JTA数据源>\r
    <类别和GT; com.restaurant.orders.Users< /班>\r
    <类别和GT; com.restaurant.orders.Orderitems< /班>\r
    <类别和GT; com.restaurant.orders.Orders< /班>\r
    <排除,非上市类>真< /排除-未列名类>\r
    <性能/>\r
  < /持久化部>\r
< /持久>

\r

\r
\r

弹簧servlet.xml中

\r
\r

<?XML版本=1.0编码=窗口-1252? >\r
<豆的xmlns =htt​​p://www.springframework.org/schema/beans\r
       XMLNS:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance\r
       的xmlns:P =htt​​p://www.springframework.org/schema/p\r
       的xmlns:上下文=htt​​p://www.springframework.org/schema/context\r
       的xmlns:MVC =htt​​p://www.springframework.org/schema/mvc\r
       的xmlns:AOP =htt​​p://www.springframework.org/schema/aop\r
       的xmlns:TX =htt​​p://www.springframework.org/schema/tx\r
       XSI:的schemaLocation =\r
       http://www.springframework.org/schema/beans\r
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\r
       http://www.springframework.org/schema/context\r
       http://www.springframework.org/schema/context/spring-context-3.0.xsd\r
       http://www.springframework.org/schema/mvc\r
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd\r
       http://www.springframework.org/schema/aop\r
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd\r
       http://www.springframework.org/schema/tx\r
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd\">\r
 \r
  <! - 使用@Component注解bean定义 - >\r
  <背景:组件扫描基包=com.restaurant.orders/>\r
 \r
  <! - 使用@Controller注解MVC控制器定义 - >\r
  < MVC:注解驱动/>\r
 \r
\r
  <! - 添加JPA支持 - >\r
  <豆的id =EMF类=\r
       org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean>\r
     <属性名=的LoadTimeWeaver>\r
        < bean类=\r
 org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver/>\r
      < /性>\r
  < /豆>\r
 \r
  <! - 添加事务支持 - >\r
  <豆的id =myTxManager\r
     类=org.springframework.orm.jpa.JpaTransactionManager>\r
        <属性名=entityManagerFactory的REF =EMF/>\r
  < /豆>\r
 \r
  <! - 使用@Transaction注解管理事务 - >\r
  < TX:注解驱动的事务管理器=myTxManager/>\r
 \r
  <! - 视图解析 - >\r
 < bean类=\r
     org.springframework.web.servlet.view.InternalResourceViewResolver>\r
   <属性名=preFIXVALUE =/ WEB-INF //>\r
 < /豆>\r
 \r
< /豆>

\r

\r
\r

追踪:

\r
\r

类型异常报告\r
\r
messageInternal服务器错误\r
\r
descriptionThe服务器遇到$ P $履行这一要求pvented它内部错误。\r
\r
例外\r
\r
org.springframework.web.util.NestedServletException:请求处理失败;嵌套的例外是org.springframework.transaction.CannotCreateTransactionException:无法打开JPA EntityManager的事务;嵌套的例外是java.lang.IllegalStateException:\r
异常说明:在使用JTA不能使用EntityTransaction。\r
根本原因\r
\r
org.springframework.transaction.CannotCreateTransactionException:无法打开JPA EntityManager的事务;嵌套的例外是java.lang.IllegalStateException:\r
异常说明:在使用JTA不能使用EntityTransaction。\r
根本原因\r
\r
java.lang.IllegalStateException:\r
异常说明:在使用JTA不能使用EntityTransaction。\r
注意异常的完整堆栈跟踪和其根源是在GlassFish Server开源版4.1日志中找到。

\r

\r
\r

任何帮助将大大AP preciated。


解决方案

添加元数据的完整=true将你的web.xml像

\r
\r

< web-app的版本=3.1的xmlns =HTTP:// xmlns.jcp.org/xml/ns/javaee的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance\r
         XSI:的schemaLocation =htt​​p://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd\r
         元数据的完整=真正的>

\r

\r
\r

I understand this query has been dealt with in other discussions, but I have not mixed @Transactional with a em.getTranstaction.begin() or any other manual transaction statement. (Using glassfish and netbeans 8)

Code below:

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>

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>

Trace:

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.

Any help would be greatly appreciated.

解决方案

add metadata-complete="true" to your web.xml like that

<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">

这篇关于在使用JTA(无需人工交易报表)不能使用EntityTransaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆