gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:55 25 4
gpt4 key购买 nike

我知道这个查询已在其他讨论中处理过,但我没有将 @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();
}

}

持久性.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.

任何帮助将不胜感激。

最佳答案

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

关于java - 使用 JTA 时无法使用 EntityTransaction(无手动事务语句),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28965877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com