使用EntityManager从数据库中删除数据会导致违反参照完整性

时间:2016-04-26 07:48:59

标签: java hibernate jpa persistence entitymanager

我正在尝试使用javax.persistence.EntityManager从数据库中删除数据。这是我的应用程序的特定摘录的类图:

Class diagram

我有一个模型类WorkOrder,其中包含Specimen。我可以创建新的WorkOrderSpecimen并将它们添加到我的数据库中。然后,JPA会自动创建表格WORKORDERSPECIMENWORKORDER_SPECIMEN。代码非常简单:

public String newWorkOrder() {
    workOrderCurrent = new WorkOrder();
    return "new_work_order?faces-redirect=true";
}

public String newSpecimen() {
    specimenCurrent = new Specimen();
    return "new_specimen?faces-redirect=true";
}

这是我坚持WorkOrderSpecimen

的方式
public String saveWorkOrder() {
    try {
        utx.begin();
        workOrderCurrent = em.merge(workOrderCurrent);
        em.persist(workOrderCurrent);
        workOrdersList.setWrappedData(em.createNamedQuery("SelectWorkOrders").getResultList());
        utx.commit();
    } catch (NotSupportedException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        e.printStackTrace();
    } catch (HeuristicRollbackException e) {
        e.printStackTrace();
    }
    return "work_orders_overview?faces-redirect=true";
}

public String saveSpecimen() {
    this.workOrderCurrent.getSpecimens().add(specimenCurrent);
    try {
        utx.begin();
        workOrderCurrent = em.merge(workOrderCurrent);
        em.persist(workOrderCurrent);
        workOrdersList.setWrappedData(em.createNamedQuery("SelectWorkOrders").getResultList());
        utx.commit();
    } catch (NotSupportedException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        e.printStackTrace();
    } catch (HeuristicRollbackException e) {
        e.printStackTrace();
    }
    return "specimens_overview?faces-redirect=true";
}

我也可以删除WorkOrder,即使它们包含Specimen s。代码:

public String deleteWorkOrder() {
    try {
        utx.begin();
    } catch (NotSupportedException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    }
    workOrderCurrent = workOrdersList.getRowData();
    workOrderCurrent = em.merge(workOrderCurrent);
    em.remove(workOrderCurrent);
    workOrdersList.setWrappedData(em.createNamedQuery("SelectWorkOrders").getResultList());
    try {
        utx.commit();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        e.printStackTrace();
    } catch (HeuristicRollbackException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    }
    return "work_orders_overview?faces-redirect=true";
}

在下一步中,我尝试从Specimen中删除WorkOrder。代码:

public String deleteSpecimen() {
    try {
        utx.begin();
    } catch (NotSupportedException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    }
    specimenCurrent = specimensList.getRowData();
    specimenCurrent = em.merge(specimenCurrent);
    em.remove(specimenCurrent);
    specimensList.setWrappedData(em.createNamedQuery("SelectSpecimens").getResultList());
    try {
        utx.commit();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (HeuristicMixedException e) {
        e.printStackTrace();
    } catch (HeuristicRollbackException e) {
        e.printStackTrace();
    } catch (SystemException e) {
        e.printStackTrace();
    }
    return "specimens_overview?faces-redirect=true";
}

调用deleteSpecimen()方法时,出现以下错误:

09:38:20,661 INFO  [stdout] (default task-20) Hibernate: select specimen0_.id as id1_5_5_, specimen0_.fiber_id as fiber_id2_5_5_, specimen0_.gelcoat_id as gelcoat_3_5_5_, specimen0_.hardener_id as hardener4_5_5_, specimen0_.holeFilm_id as holeFilm5_5_5_, specimen0_.releaseAgent_id as releaseA6_5_5_, specimen0_.workOrder_id as workOrde7_5_5_, fiber1_.id as id1_0_0_, fiber1_.chargeNr as chargeNr2_0_0_, fiber1_.description as descript3_0_0_, fiber1_.manufacturer as manufact4_0_0_, fiber1_.surfaceWeight as surfaceW5_0_0_, fiber1_.type as type6_0_0_, gelcoat2_.id as id1_1_1_, gelcoat2_.chargeNr as chargeNr2_1_1_, gelcoat2_.description as descript3_1_1_, gelcoat2_.manufacturer as manufact4_1_1_, hardener3_.id as id1_2_2_, hardener3_.chargeNr as chargeNr2_2_2_, hardener3_.description as descript3_2_2_, hardener3_.hardnessType as hardness4_2_2_, hardener3_.manufacturer as manufact5_2_2_, holefilm4_.id as id1_3_3_, holefilm4_.chargeNr as chargeNr2_3_3_, holefilm4_.description as descript3_3_3_, holefilm4_.manufacturer as manufact4_3_3_, releaseage5_.id as id1_4_4_, releaseage5_.chargeNr as chargeNr2_4_4_, releaseage5_.description as descript3_4_4_, releaseage5_.manufacturer as manufact4_4_4_ from Specimen specimen0_ left outer join Fiber fiber1_ on specimen0_.fiber_id=fiber1_.id left outer join Gelcoat gelcoat2_ on specimen0_.gelcoat_id=gelcoat2_.id left outer join Hardener hardener3_ on specimen0_.hardener_id=hardener3_.id left outer join HoleFilm holefilm4_ on specimen0_.holeFilm_id=holefilm4_.id left outer join ReleaseAgent releaseage5_ on specimen0_.releaseAgent_id=releaseage5_.id where specimen0_.id=?

09:38:20,669 INFO  [stdout] (default task-20) Hibernate: delete from Specimen where id=?

09:38:20,671 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-20) SQL Error: 23503, SQLState: 23503
09:38:20,671 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-20) Referentielle Integrität verletzt: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"
Referential integrity constraint violation: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"; SQL statement:
delete from Specimen where id=? [23503-173]
09:38:20,673 INFO  [org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl] (default task-20) HHH000010: On release of batch it still contained JDBC statements
09:38:20,674 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-20) #{mbWorkOrderController.deleteSpecimen(Specimen)}: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement: javax.faces.FacesException: #{mbWorkOrderController.deleteSpecimen(Specimen)}: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIData.broadcast(UIData.java:1108)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 35 more
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458)
    at de.fraunhofer.iwes.controller.WorkOrderController.deleteSpecimen(WorkOrderController.java:269)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:308)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:286)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 36 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
    at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:67)
    at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1227)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1293)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
    ... 52 more
Caused by: org.h2.jdbc.JdbcSQLException: Referentielle Integrität verletzt: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"
Referential integrity constraint violation: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"; SQL statement:
delete from Specimen where id=? [23503-173]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
    at org.h2.message.DbException.get(DbException.java:171)
    at org.h2.message.DbException.get(DbException.java:148)
    at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:421)
    at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:438)
    at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:314)
    at org.h2.table.Table.fireConstraints(Table.java:880)
    at org.h2.table.Table.fireAfterRow(Table.java:897)
    at org.h2.command.dml.Delete.update(Delete.java:100)
    at org.h2.command.CommandContainer.update(CommandContainer.java:79)
    at org.h2.command.Command.executeUpdate(Command.java:235)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:154)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:140)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:537)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 65 more

09:38:20,679 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (default task-20) javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIData.broadcast(UIData.java:1108)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:458)
    at de.fraunhofer.iwes.controller.WorkOrderController.deleteSpecimen(WorkOrderController.java:269)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.el.ELUtil.invokeMethod(ELUtil.java:308)
    at javax.el.BeanELResolver.invoke(BeanELResolver.java:537)
    at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:286)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 36 more
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
    at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400)
    at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630)
    at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:465)
    at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:351)
    at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)
    at org.hibernate.event.internal.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:67)
    at org.hibernate.internal.SessionImpl.autoFlushIfRequired(SessionImpl.java:1227)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1293)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
    ... 52 more
Caused by: org.h2.jdbc.JdbcSQLException: Referentielle Integrität verletzt: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"
Referential integrity constraint violation: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"; SQL statement:
delete from Specimen where id=? [23503-173]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:331)
    at org.h2.message.DbException.get(DbException.java:171)
    at org.h2.message.DbException.get(DbException.java:148)
    at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:421)
    at org.h2.constraint.ConstraintReferential.checkRowRefTable(ConstraintReferential.java:438)
    at org.h2.constraint.ConstraintReferential.checkRow(ConstraintReferential.java:314)
    at org.h2.table.Table.fireConstraints(Table.java:880)
    at org.h2.table.Table.fireAfterRow(Table.java:897)
    at org.h2.command.dml.Delete.update(Delete.java:100)
    at org.h2.command.CommandContainer.update(CommandContainer.java:79)
    at org.h2.command.Command.executeUpdate(Command.java:235)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:154)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:140)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:537)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
    ... 65 more

09:38:20,681 ERROR [org.jboss.as.txn] (default task-20) WFLYTX0003: APPLICATION ERROR: transaction still active in request with status 1

这意味着我违反了参照完整性。我做错了什么?

2 个答案:

答案 0 :(得分:0)

同意Scary Wombat

这里很清楚,你有WorkOrder仍然指的是Specimen。您可以在表约束中添加级联ON DELETE。

09:38:20,671 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-20) Referentielle Integrität verletzt: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"
Referential integrity constraint violation: "FK_DW0025YHI9UYOL04FFU8OR0JY: PUBLIC.WORKORDER_SPECIMEN FOREIGN KEY(SPECIMENS_ID) REFERENCES PUBLIC.SPECIMEN(ID) (3)"; SQL statement:

答案 1 :(得分:0)

您说您正在从工作订单中删除标本,但您只是在标本实例上调用删除。您需要实际从工作订单中删除引用并合并参考表的工作订单以反映更改。

如果您正在使用孤立删除,那么这足以从数据库中删除Specimen实例。否则,您应该调用em.remove(标本)将其从数据库中删除。