问题
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.
回答1:
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">
来源:https://stackoverflow.com/questions/28965877/cannot-use-an-entitytransaction-while-using-jta-no-manual-transaction-statement