JPA删除查询在EJB中不起作用

时间:2014-10-09 04:58:07

标签: jpa-2.0

我试图在无状态EJB中使用JPQL删除实体。以下是我的代码 -

@PersistenceContext(的unitName =" MySQL的")
    protected EntityManager em;

查询query = null;

@Stateless

public class Test {     public void delete(){

    query = em.createQuery("delete from JPAAgentInfo a WHERE a.client_id=?1 and a.agent_id=?2");
    query.setParameter(1, 111);
    query.setParameter(2, 1);       
    query.executeUpdate();    
}

}

但是上面的代码会引发异常 -

  

javax.persistence.TransactionRequiredException:执行更新/删除查询   10:25:17,503 ERROR [io.undertow.request](默认任务-1)UT005023:对/ servlet / delete的异常处理请求:javax.persistence.TransactionRequiredException:执行更新/删除查询           在org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)[hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]           在org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.executeUpdate(QueryNonTxInvocationDetacher.java:80)[wildfly-jpa-8.1.0.Final.jar:8.1.0.Final]           at com.bitbyte.servlet.ChatServer.doGet(ChatServer.java:81)[classes:]           在javax.servlet.http.HttpServlet.service(HttpServlet.java:687)[jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]           在javax.servlet.http.HttpServlet.service(HttpServlet.java:790)[jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]           at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)           at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final           at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           在org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)           at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           在io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.servlet.handlers.ServletInitialHandler.access $ 000(ServletInitialHandler.java:73)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           在io.undertow.servlet.handlers.ServletInitialHandler $ 1.handleRequest(ServletInitialHandler.java:146)[undertow-servlet-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at io.undertow.server.HttpServerExchange $ 1.run(HttpServerExchange.java:727)[undertow-core-1.0.15.Final.jar:1.0.15.Final]           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)[rt.jar:1.7.0_65]           at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)[rt.jar:1.7.0_65]           在java.lang.Thread.run(Thread.java:745)[rt.jar:1.7.0_65]

1 个答案:

答案 0 :(得分:1)

它说“javax.persistence.TransactionRequiredException”。

如果没有事务,则无法运行数据库更新(仅允许查询)。

执行此操作的一种方法是使用@Transactional注释。