持久化实体与多对多关联实体抛出 "org.hibernate.PersistentObjectException"

Persist Entity with Many-To-Many association Entity throws "org.hibernate.PersistentObjectException"

我想保留一个新的 TestRun 实体,它有新的 TestRunTestCase 子实体,但我得到了一个 org.hibernate.PersistentObjectException,我不明白为什么。

TestRun.java:

    @Entity
    @Table(name = "testrun")
    public class TestRun {
        
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private int id;
        
        @Column(name = "name", nullable = false)
        private String name;
        
        @ManyToOne
        @JoinColumn(name = "status_id", nullable=false)
        private Status status;
        
        @ManyToOne
        @JoinColumn(name = "user_id")
        private User assignee;
        
        @ManyToOne
        @JoinColumn(name = "requirement_id", nullable=false)
        private Requirement requirement;
        
        @OneToMany(mappedBy = "testrun", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        private List<TestRunTestCase> testcaseAssoc = new ArrayList<>();

public void addTestcaseAssoc(TestRunTestCase trtc) {
    testcaseAssoc.add(trtc);
    trtc.setTestrun(this);
}

public void removeTestcaseAssoc(TestRunTestCase trtc) {
    testcaseAssoc.remove(trtc);
    trtc.setTestrun(null);
}

TestCase.java:

@Entity
@Table(name = "testcase")
public class TestCase {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    
    @Column(name = "name", nullable = false)
    private String name;
    
    @Column(name = "description", nullable = false)
    private String description;
    
    @ManyToOne
    @JoinColumn(name = "requirement_id", nullable=false)
    private Requirement requirement;
    
    @OneToMany(mappedBy = "testcase", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<TestRunTestCase> testrunAssoc = new ArrayList<>();

试运行TestCase.java:

@Entity
@Table(name = "testrun_testcase")
@IdClass(TestRunTestCaseId.class)
public class TestRunTestCase {

    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "testrun_id", referencedColumnName = "id")
    private TestRun testrun;
    
    @Id
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "testcase_id", referencedColumnName = "id")
    private TestCase testcase;
    
    @ManyToOne
    @JoinColumn(name = "status_id", nullable=false)
    private Status status;
    

方法 createTestRun():

// ... created new TestRun Object -> newTestRun
// ... get existing List of TestCases -> testCaseList
// ... get existing initial Status -> caseStatus
    testCaseList.forEach(c -> newTestRun.addTestcaseAssoc(new TestRunTestCase(newTestRun, c, caseStatus)));
    testRunDao.save(newTestRun);

TestRunDao.java:

public class TestRunDao implements Dao<TestRun> {

    @Override
    public Optional<TestRun> get(int id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Optional<TestRun> testRun = Optional.ofNullable(session.find(TestRun.class, id));
        session.close();
        return testRun;
    }

    @Override
    public List<TestRun> getAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        List<TestRun> testRunList = session.createQuery("FROM TestRun u ", TestRun.class).getResultList();
        session.close();
        return testRunList;
    }

    @Override
    public void save(TestRun t) {
        executeTransaction(entityManager -> entityManager.persist(t));
        
    }

    @Override
    public void update(TestRun t) {
        executeTransaction(entityManager -> entityManager.merge(t));
        
    }

    @Override
    public void delete(TestRun t) {
        executeTransaction(entityManager -> entityManager.remove(t));
        
    }   
    
    private void executeTransaction(Consumer<EntityManager> action) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        EntityTransaction tx = session.getTransaction();
        try {
            tx.begin();
            action.accept(session);
            tx.commit(); 
        }
        catch (RuntimeException e) {
            tx.rollback();
            throw e;
        }
        finally {
            session.close();
        }
    }

}

错误:

javax.faces.FacesException: #{requirementDetails.createTestRun}: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.iu.ticketsystem.entity.TestCase
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    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:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at com.iu.ticketsystem.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:43)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:540)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:359)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:889)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1735)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javax.faces.el.EvaluationException: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.iu.ticketsystem.entity.TestCase
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 32 more
Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.iu.ticketsystem.entity.TestCase
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
    at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:188)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:786)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
    at org.hibernate.engine.spi.CascadingActions.cascade(CascadingActions.java:298)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:510)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:434)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:220)
    at org.hibernate.engine.internal.Cascade.cascadeComponent(Cascade.java:405)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:241)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:153)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:427)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:264)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:135)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:128)
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:118)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)
    at org.hibernate.engine.spi.CascadingActions.cascade(CascadingActions.java:298)
    at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:510)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:434)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:220)
    at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:543)
    at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:474)
    at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:437)
    at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:220)
    at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:153)
    at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:459)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:293)
    at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:123)
    at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:128)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:55)
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:107)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:756)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:742)
    at com.iu.ticketsystem.dao.TestRunDao.lambda[=16=](TestRunDao.java:36)
    at com.iu.ticketsystem.dao.TestRunDao.executeTransaction(TestRunDao.java:57)
    at com.iu.ticketsystem.dao.TestRunDao.save(TestRunDao.java:36)
    at com.iu.ticketsystem.beans.RequirementDetails.createTestRun(RequirementDetails.java:153)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:252)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:266)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 33 more
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.iu.ticketsystem.entity.TestCase
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:120)
    at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:118)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780)
    ... 81 more

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.iu.ticketsystem.entity.TestCase

我该如何解决这个问题?

谢谢。

我找到了解决问题的简单方法。 我在我的连接中添加了一个 id table 并像这样映射它:

@Entity
@Table(name = "testrun_testcase")
public class TestRunTestCase {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "testrun_id", referencedColumnName = "id")
    private TestRun testrun;
    
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "testcase_id", referencedColumnName = "id")
    private TestCase testcase;
    
    @ManyToOne
    @JoinColumn(name = "status_id", nullable=false)
    private Status status;
    

所以我不再将 testrun 和 testcase 属性用作 id,而是使用单独的 id。