2013-02-26 15 views
12

Ich möchte alle Zeilen aus einer bestimmten Tabelle mit JPA entfernen.Entfernen aller Zeilen aus einer Tabelle mit JPA

Was ich tat: public class EmptyBomTables erweitert 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(); 
    } 
} 

Fehler:

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 

Was mache ich falsch?

+2

Ist das nicht: 'TransactionRequiredException: Ausführen von Update/löschen Sie einen Hinweis query' geben? –

+0

Es sagt, dass eine Transaktion erforderlich ist, deshalb habe ich utx.begin und utx.commit – doonot

+0

hinzugefügt Und 'utx' ist was? Auch warum "try-catch" Blöcke trennen? –

Antwort

9

, dass das Problem war, hat alles, was in der gleichen try/catch sein:

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(); 
} 
+6

Da die Frage 'createQuery' verwendet wurde, sollte Ihre Antwort auch' createQuery' anstelle von 'createNativeQuery' verwenden –

Verwandte Themen