當我嘗試刪除ExternalDataStorage此查詢拋出一個約束衝突異常約束名爲null .ExternalDataStorage沒有關係映射關於它。查詢首先檢查ExternalTasks其中包含ExternalDataStorage(將被刪除)在ExternalTask的externalsources列表並從列表中移除,如果列表爲空,則移除externaltask並最終移除ExternalDataStorage實體(目標實體)。JPA @manytomany單向映射
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;
}
@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(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;
_編輯_ __ _ __ _ __ _ __ _ __ _ ___
更改查詢有點爲刪除的情況下,但問題仍然存在
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;
}
這是例外
**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)
引起:java.sql.BatchUpdateException :批次條目0從其中EXID = 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) 在org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674) 在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) 的組織。 hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) ... 55更多 javax.persistence.RollbackException:在org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:71)提交事務 時出錯。 在com.endersys.itap.ui.module.storage.StorageManager.deleteExternalDataStorage(StorageManager.java:351) 在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) 在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) 在com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpres sion.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) at org.ajax4jsf.component.AjaxActionComponent.broadcast(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) 在有機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.MyFacesServ let.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) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206) at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilt er.java:290) 在org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388) 在org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515) 在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) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 在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) 在org.apache.tomcat。 util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:636) 原因:org.hibernate.exception.ConstraintViolationException:無法在org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) 處執行JDBC批量更新 (位於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) 在org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) 在org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) 在有機hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) 在org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) 在org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:54) ... 47更多 原因: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) 在org.postgresql.jdbc2.AbstractJdbc2Statement.executeBatch(AbstractJdbc2Statement.java:2674) 在org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48) 的組織。 hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242) ...... 55多個
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;
ExternalSource而ExternalDataStorage是同一個實體存在hell.Sorry ... – ayengin 2011-06-09 20:52:16