使用JPA从表中删除所有行 [英] Removing all rows from a table using JPA

查看:129
本文介绍了使用JPA从表中删除所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JPA从特定表中删除所有行。

I want to remove all rows from a specific table using JPA.

我做了什么:
public class EmptyBomTables extends HttpServlet {

What I did: public class EmptyBomTables extends HttpServlet {

private static final long serialVersionUID = 1L;

@PersistenceContext(unitName = "xxx")
public EntityManager em;

@Resource
UserTransaction utx;

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Query q1 = em.createQuery("DELETE FROM BomModule");
    Query q2 = em.createQuery("DELETE FROM BomItem");
    Query q3 = em.createQuery("DELETE FROM ItemModuleConnection");
    Query q4 = em.createQuery("DELETE FROM ModuleConnection");

    try {
        utx.begin();
    } catch (NotSupportedException | SystemException e) {
        e.printStackTrace();
    }

    q1.executeUpdate();
    q2.executeUpdate();
    q3.executeUpdate();
    q4.executeUpdate();

    try {
        utx.commit();
    } catch (SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException | SystemException e) {
        e.printStackTrace();
    }
}

错误:

   09:30:30,197 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SSIS2].[EmptyBomTables]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet EmptyBomTables threw exception: javax.persistence.TransactionRequiredException: Executing an update/delete query
    at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]
    at org.jboss.as.jpa.container.QueryNonTxInvocationDetacher.executeUpdate(QueryNonTxInvocationDetacher.java:80) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at com.sicap.ssis2.bom.EmptyBomTables.doGet(EmptyBomTables.java:50) [classes:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:397) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_09]

09:30:30,218 ERROR [org.jboss.as.txn] (http-localhost-127.0.0.1-8080-1) JBAS010152: APPLICATION ERROR: transaction still active in request with status 1

我做错了什么?

推荐答案

一切都必须在同一个try / catch:

That was the problem, everything has to be in the same try/catch:

try {

    utx.begin();

    Query q3 = em.createNativeQuery("DELETE FROM ItemModuleConnection");
    Query q4 = em.createNativeQuery("DELETE FROM ModuleConnection");
    Query q1 = em.createNativeQuery("DELETE FROM BomModule");
    Query q2 = em.createNativeQuery("DELETE FROM BomItem");

    q1.executeUpdate();
    q2.executeUpdate();
    q3.executeUpdate();
    q4.executeUpdate();

    utx.commit();
} catch (NotSupportedException | SystemException | SecurityException | IllegalStateException | RollbackException | HeuristicMixedException | HeuristicRollbackException e) {
    e.printStackTrace();
}

这篇关于使用JPA从表中删除所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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