本文介绍了JPA @manytomany单向映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试删除ExternalDataStorage这个查询抛出一个约束冲突异常,约束名为null .ExternalDataStorage没有关系映射。查询首先在ExternalTask​​的externalsources列表中检查ExternalTask​​s是否包含ExternalDataStorage(将被删除)并从列表中移除,如果列表为空,则移除externaltask并最终移除ExternalDataStorage实体(目标实体)。

  public boolean deleteExternalDataStorage(Long sid){
EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();
尝试{
et.begin();
ExternalDataStorage s = em.find(ExternalDataStorage.class,sid);
列表< ExternalTask​​> tasks =(List< ExternalTask​​>)em.createQuery(SELECT t FROM ExternalTask​​ t).getResultList(); (b)(
for(ExternalTask​​ t:tasks){
if(t.getExternalSources()。contains(s)){
t.getExternalSources()。remove(s);
if(t.getExternalSources()。isEmpty()){
em.remove(t);
} else {
t = em.merge(t);
}
}
}
em.remove(s);
et.commit();
返回true;
} catch(Exception e){
e.printStackTrace();

$ b}最后{
if(et.isActive()){
et.rollback();
}
}
返回false;





@Entity
公共类ExternalTask​​实现Serializable {

  @ManyToMany(cascade = {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name =ExternalTask​​_ExternalSource ,joinColumns = @JoinColumn(name =TID),inverseJoinColumns = @JoinColumn(name =EXID))
private List< ExternalDataStorage> externalSources = new ArrayList< ExternalDataStorage>();


@ManyToMany(级联= {CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(名称= ExternalTask​​_Archive,joinColumns = @JoinColumn(名=TID),inverseJoinColumns = @JoinColumn(name =AID​​))
protected List< Archive> archives = new ArrayList< Archive>();


@ManyToOne
私人用户ownerUser;

_ EM>的 _ __ _ __ _ __ _ __ _ __ ___





  EntityManager em = getEntityManager(); 
EntityTransaction et = em.getTransaction();
尝试{
et.begin();
ExternalDataStorage s = em.find(ExternalDataStorage.class,sid);
列表< ExternalTask​​> tasks =(List< ExternalTask​​>)em.createQuery(SELECT t FROM ExternalTask​​ t).getResultList(); (b)(
for(ExternalTask​​ t:tasks){
if(t.getExternalSources()。contains(s)){
t.getExternalSources()。remove(s);
t = em.merge(t);
if(t.getExternalSources()。isEmpty()){
em.remove(t);
}
}
}
em.remove(s);
et.commit();
返回true;
} catch(Exception e){
e.printStackTrace();

$ b}最后{
if(et.isActive()){
et.rollback();
}
}
返回false;
}

这是个例外

  ** 377249 [http-8080-2] WARN org.hibernate.util.JDBCExceptionReporter  -  SQL错误:0,SQLState:null 
377249 [http-8080-2] ERROR org.hibernate.util.JDBCExceptionReporter - 批次条目0从其中EXID = 6被中止的ExternalSource中删除。调用getNextException来查看原因。
377249 [HTTP-8080-2] WARN org.hibernate.util.JDBCExceptionReporter - SQL错误:0,SQLSTATE:23503
377249 [HTTP-8080-2] ERROR org.hibernate.util.JDBCExceptionReporter - 错误:更新或删除表externalsource违反了表externaltask_externalsource上的外键约束fk755287f526733b01
详细信息:表(exid)=(6)仍从表externaltask_externalsource引用。
377250 [http-8080-2] ERROR org.hibernate.event.def.AbstractFlushingEventListener - 无法使数据库状态与会话
同步org.hibernate.exception.ConstraintViolationException:无法执行JDBC批更新
在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
在org.hibernate.jdbc .AbstractBatcher.executeBatch(AbstractBatcher.java:249)
在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java :144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
在org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
在org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl .commit(TransactionImpl.java:54)
at com.endersys.itap.ui.module.storage.StorageManager.deleteExternalDataStorage(StorageManager.java:351)
at com.endersys.itap.ui.module .storage.ExternalDataStorageEditorBean.delete(ExternalDataStorageEditorBean.java:73)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.el.parser .AstValue.invoke(AstValue.java:172)
在org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
在com.sun.facelets.el.TagMethodExpression.in voke(TagMethodExpression.java:68)
在javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:78)
在org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java: 57)美元,javax.faces.component.UICommand.broadcast(UICommand.java:127 b $ b)
在org.ajax4jsf.component.AjaxActionComponent.broadcast(AjaxActionComponent.java:55)
。在组织.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
在org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
在org.ajax4jsf.component.AjaxViewRoot.processPhase (AjaxViewRoot.java:261)
在org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
在org.apache.my ** faces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor。 java:32)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103)
at org.apac he.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
位于javax.faces.webapp.FacesServlet.service(FacesServlet.java:183)
位于org.apache.myfaces.webapp。 MyFacesServlet.service(MyFacesServlet.java:103)
在com.endersys.itap.ui.MyFacesServletWrapper.service(MyFacesServletWrapper.java:34)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter( ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter。
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:29 0)
。在org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
。在org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
。在有机.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.endersys.itap .ui.MenuFilter.doFilter(MenuFilter.java:169)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain .doFilter(ApplicationFilterChain.java:206)
在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve .java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorRepor tValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service( CoyoteAdapter.java:298)
在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol。 java:588)
at org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:636)

原因:java.sql.BatchUpdateException:批次条目0从ExternalSource中删除EXID = 6被中止。调用getNextException来查看原因。
at org.postgresql.jdbc2.AbstractJdbc2Statement $ BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
在org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674)
at org.hibernate.jdbc .BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 55 more
javax.persistence.RollbackException :在提交事务
时发生错误org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)
at com.endersys.itap.ui.module.storage.StorageManager.deleteExternalDataStorage(StorageManager。 java:351)
at com.endersys.itap.ui.module.storage.ExternalDataStorageEditorBean.delete (ExternalDataStorageEditorBean.java:73)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
在sun.reflect .DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.el.parser.AstValue.invoke(AstValue .java:172)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:78)
at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
在javax。 faces.component.UICommand.broadcast(UICommand.java:127)
at org.ajax4jsf.component.AjaxActionComponent.broad投(AjaxActionComponent.java:55)
在org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
在org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)在org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)

在org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
在org.apache .myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103)
at org.apache.myfaces.lifecycle .LifecycleImpl.execute(LifecycleImpl.java:76)
在javax.faces.webapp.FacesServlet.service(FacesServlet.java:183)在org.apache.myfaces.webapp.MyFacesServlet.service
(MyFacesServlet .java:103)
at com.endersys.itap.ui.MyFacesServletWrapper.service(MyFacesServletWrapper.java:34)
at org.apache.ca在org.apache.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
(位于org.apache.myfaces.webapp处)
。 filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
处org.apache.catalina.core.ApplicationFilterChain org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
。的doFilter(ApplicationFilterChain.java:206)
在org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
在org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
at org.apache .catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.ca talina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.endersys.itap.ui.MenuFilter.doFilter(MenuFilter.java:169)
at org.apache.catalina.core。 ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
在org.apache.catalina.core.StandardWrapperValve.invoke( StandardWrapperValve.java:233)美元,org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191 b $ b)
在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)$ b在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
$ b at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11 Processor.process(Http11Processor.java:852)
at org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net。 JIoEndpoint $ Worker.run(JIoEndpoint.java:489)
在java.lang.Thread.run(Thread.java:636)
引起:org.hibernate.exception.ConstraintViolationException:无法执行JDBC在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)批量更新

在org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
。在组织.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
在org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
在org.hibernate.engine.ActionQueue.executeActions (ActionQueue.java:144)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
处org.hibernate.impl org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
。在org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:$)中, 54)
... 47 more
原因:java.sql.BatchUpdateException:批处理条目0从EXS = 6被中止的ExternalSource中删除。调用getNextException来查看原因。
在org.postgresql.jdbc2.AbstractJdbc2Statement $ BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537)
在org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328)
在org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674)
at org.hibernate.jdbc .BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 55 more

 这是对表



的约束ALTER TABLE externaltask_externalsource
ADD CONSTRAINT fk755287f526733b01 FOREIGN KEY( exid)
REFERENCES externalsource(exid)MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION;


解决方案

我添加连接抓取并解决问题。

  public boolean deleteExternalDataStorage(Long sid){
EntityManager em = getEntityManager();
EntityTransaction et = em.getTransaction();

尝试{
et.begin();
ExternalDataStorage s = em.find(ExternalDataStorage.class,sid);
列表< ExternalTask​​> tasks =(List< ExternalTask​​>)em.createQuery(SELECT t FROM ExternalTask​​ t join fetch t.externalSources)。getResultList(); (b)(
for(ExternalTask​​ t:tasks){
if(t.getExternalSources()。contains(s)){
t.getExternalSources()。remove(s);
if(t.getExternalSources()。isEmpty()){
em.remove(t);
}
}
}
em.remove(s);
et.commit();
返回true;
} catch(Exception e){
e.printStackTrace();
if(et.isActive()){
et.rollback();
}

} finally {

}
return false;
}


When I try remove ExternalDataStorage This query throw a constraint violation exception with constraint name null .ExternalDataStorage has no relation mapping on it.Query first check ExternalTasks whichs contain ExternalDataStorage(Which will be removed) in ExternalTask's externalsources List and remove from list, if list is empty remove externaltask and finally remove ExternalDataStorage entity(Target entity).

      public boolean deleteExternalDataStorage(Long sid) {
     EntityManager em = getEntityManager();
    EntityTransaction et = em.getTransaction();
    try {
        et.begin();
        ExternalDataStorage s = em.find(ExternalDataStorage.class, sid);
        List<ExternalTask> tasks = (List<ExternalTask>) em.createQuery("SELECT t FROM ExternalTask t ").getResultList();
        for (ExternalTask t : tasks) {
            if (t.getExternalSources().contains(s)) {
                t.getExternalSources().remove(s);
                if (t.getExternalSources().isEmpty()) {
                    em.remove(t);
                } else {
                    t = em.merge(t);
                }
            }
        }
        em.remove(s);
        et.commit();
        return true;
    } catch (Exception e) {
        e.printStackTrace();


    } finally {
        if (et.isActive()) {
            et.rollback();
        }
    }
    return false;
}

@Entitypublic class ExternalTask implements Serializable {

@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name = "ExternalTask_ExternalSource", joinColumns = @JoinColumn(name = "TID"), inverseJoinColumns = @JoinColumn(name = "EXID"))
private  List<ExternalDataStorage> externalSources =new ArrayList<ExternalDataStorage>();


@ManyToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinTable(name = "ExternalTask_Archive", joinColumns = @JoinColumn(name = "TID"), inverseJoinColumns = @JoinColumn(name = "AID"))
protected  List<Archive> archives=new ArrayList<Archive>();


@ManyToOne
private User  ownerUser;

_EDIT___________________

I change query a little for delete case but problem still there

  EntityManager em = getEntityManager();
        EntityTransaction et = em.getTransaction();
        try {
            et.begin();
            ExternalDataStorage s = em.find(ExternalDataStorage.class, sid);
            List<ExternalTask> tasks=(List<ExternalTask>) em.createQuery("SELECT t FROM ExternalTask t ").getResultList();
           for(ExternalTask t:tasks) {
              if(t.getExternalSources().contains(s)){
                 t.getExternalSources().remove(s);
                  t= em.merge(t);
                  if(t.getExternalSources().isEmpty()){
                      em.remove(t);
                  }
               }
           }
            em.remove(s);
            et.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();


        } finally {
            if (et.isActive()) {
                et.rollback();
            }
        }
        return false;
    }

This is exception

**377249 [http-8080-2] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 0, SQLState: null
377249 [http-8080-2] ERROR org.hibernate.util.JDBCExceptionReporter  - Batch entry 0 delete from ExternalSource where EXID=6 was aborted.  Call getNextException to see the cause.
377249 [http-8080-2] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 0, SQLState: 23503
377249 [http-8080-2] ERROR org.hibernate.util.JDBCExceptionReporter  - ERROR: update or delete on table "externalsource" violates foreign key constraint "fk755287f526733b01" on table "externaltask_externalsource"
  Detail: Key (exid)=(6) is still referenced from table "externaltask_externalsource".
377250 [http-8080-2] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
        at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
        at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144)
        at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54)
    at com.endersys.itap.ui.module.storage.StorageManager.deleteExternalDataStorage(StorageManager.java:351)
    at com.endersys.itap.ui.module.storage.ExternalDataStorageEditorBean.delete(ExternalDataStorageEditorBean.java:73)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:172)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
    at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
    at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:78)
    at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57)
    at javax.faces.component.UICommand.broadcast(UICommand.java:127)
    at org.ajax4jsf.component.AjaxActionComponent.broadcast(AjaxActionComponent.java:55)
    at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329)
    at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304)
    at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261)
    at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474)
    at org.apache.my**faces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
    at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103)
    at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:183)
    at org.apache.myfaces.webapp.MyFacesServlet.service(MyFacesServlet.java:103)
    at com.endersys.itap.ui.MyFacesServletWrapper.service(MyFacesServletWrapper.java:34)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206)
    at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
    at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
    at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.endersys.itap.ui.MenuFilter.doFilter(MenuFilter.java:169)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:636)

Caused by: java.sql.BatchUpdateException: Batch entry 0 delete from ExternalSource where EXID=6 was aborted. Call getNextException to see the cause. at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) ... 55 morejavax.persistence.RollbackException: Error while commiting the transaction at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71) at com.endersys.itap.ui.module.storage.StorageManager.deleteExternalDataStorage(StorageManager.java:351) at com.endersys.itap.ui.module.storage.ExternalDataStorageEditorBean.delete(ExternalDataStorageEditorBean.java:73) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) at org.apache.el.parser.AstValue.invoke(AstValue.java:172) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) at com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68) at javax.faces.component._MethodExpressionToMethodBinding.invoke(_MethodExpressionToMethodBinding.java:78) at org.apache.myfaces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:57) at javax.faces.component.UICommand.broadcast(UICommand.java:127) at org.ajax4jsf.component.AjaxActionComponent.broadcast(AjaxActionComponent.java:55) at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329) at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:304) at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261) at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:474) at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32) at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:103) at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:76) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:183) at org.apache.myfaces.webapp.MyFacesServlet.service(MyFacesServlet.java:103) at com.endersys.itap.ui.MyFacesServletWrapper.service(MyFacesServletWrapper.java:34) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.myfaces.webapp.filter.ExtensionsFilter.doFilter(ExtensionsFilter.java:341) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206) at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290) at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388) at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.endersys.itap.ui.MenuFilter.doFilter(MenuFilter.java:169) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:852) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:636)Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:144) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) ... 47 moreCaused by: java.sql.BatchUpdateException: Batch entry 0 delete from ExternalSource where EXID=6 was aborted. Call getNextException to see the cause. at org.postgresql.jdbc2.AbstractJdbc2Statement$BatchResultHandler.handleError(AbstractJdbc2Statement.java:2537) at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1328) at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:351) at org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674) at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) ... 55 more

And this is constraint on table



ALTER TABLE externaltask_externalsource
  ADD CONSTRAINT fk755287f526733b01 FOREIGN KEY (exid)
      REFERENCES externalsource (exid) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION;
解决方案

I add join fetch and problem solved.

public boolean deleteExternalDataStorage(Long sid) {
        EntityManager em = getEntityManager();
        EntityTransaction et = em.getTransaction();

        try {
            et.begin();
            ExternalDataStorage s = em.find(ExternalDataStorage.class, sid);
            List<ExternalTask> tasks=(List<ExternalTask>) em.createQuery("SELECT t FROM ExternalTask t join fetch t.externalSources").getResultList();
           for(ExternalTask t:tasks) {
              if(t.getExternalSources().contains(s)){
                      t.getExternalSources().remove(s);
                  if(t.getExternalSources().isEmpty()){
                      em.remove(t);
                  }
               }
           }
            em.remove(s);
            et.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
             if (et.isActive()) {
                et.rollback();
            }

        } finally {

        }
        return false;
    }

这篇关于JPA @manytomany单向映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:47