分离的实体在插入新记录时传递给持久性错误

时间:2018-10-15 19:29:02

标签: java hibernate jpa

我尽力解释了这个问题,如果您需要更多信息,请告诉我。

emphist表的实体具有唯一的键,其设置如下:

@Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="EMP_SERIAL_GERERATOR")
    @SequenceGenerator(name="EMP_SERIAL_GERERATOR", sequenceName="EMP_SERIAL",  allocationSize=1)
    @Column(name="EMP_SERIAL", nullable=false, precision=9)
    private long empSerial;



try {
            em.persist(EmphistNew);
        } catch (Exception e) {
            throw e;
        }

在代码中,我试图将新记录保存到emphist表中,但出现以下错误。

javax.persistence.PersistenceException:org.hibernate.PersistentObjectException:传递给持久对象的分离实体:com.company.dep.project.model.emphist     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1152)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]     在com.company.dep.project.batch.process.CheckandfillgapProcessor.insertTransaction(CheckandfillgapProcessor.java:428)上[classes /:na]     在com.company.dep.project.batch.process.CheckandfillgapProcessor.checkForGaps(CheckandfillgapProcessor.java:247)[classes /:na]     在com.company.dep.project.batch.process.CheckandfillgapProcessor.process(CheckandfillgapProcessor.java:103)[classes /:na]     在com.company.dep.project.batch.process.test.CheckandfillgapProcessorTest.testCheckForGaps2(CheckandfillgapProcessorTest.java:129)上[test-classes /:na]     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_51]     在sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_51]     在sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)〜[na:1.8.0_51]     在java.lang.reflect.Method.invoke(未知来源)〜[na:1.8.0_51]     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)[junit-4.12.jar:4.12]     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)[junit-4.12.jar:4.12]     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)[junit-4.12.jar:4.12]     在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)[junit-4.12.jar:4.12]     在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)[junit-4.12.jar:4.12]     在org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)[junit-4.12.jar:4.12]     在org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)[junit-4.12.jar:4.12]     在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)上[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)[junit-4.12.jar:4.12]     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)[junit-4.12.jar:4.12]     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)[junit-4.12.jar:4.12]     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)[junit-4.12.jar:4.12]     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)[junit-4.12.jar:4.12]     在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)[junit-4.12.jar:4.12]     在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)[junit-4.12.jar:4.12]     在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)[junit-4.12.jar:4.12]     在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)上[spring-test-4.3.18.RELEASE.jar:4.3.18.RELEASE]     在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)[.cp /:na]     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)[.cp /:na]     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)[.cp /:na]     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)[.cp /:na]     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)[.cp /:na]     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)[.cp /:na] 引起原因:org.hibernate.PersistentObjectException:传递给持久对象的分离实体:com.company.dep.project.model.emphist     在org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:124)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:775)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.internal.SessionImpl.persist(SessionImpl.java:748)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.internal.SessionImpl.persist(SessionImpl.java:753)〜[hibernate-core-5.0.12.Final.jar:5.0.12.Final]     在org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1146)〜[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]     ...省略了37个常见框架

3 个答案:

答案 0 :(得分:0)

不看完整的代码就很难说什么。但唯一的例外是自我描述:

detached entity passed to persist: com.company.dep.project.model.emphist

因此,在调用persist()时已经分配了ID字段,这是不允许的。如果对象存在,则需要调用merge()来修改其状态。或者,您需要找出在代码中设置ID字段的位置。

答案 1 :(得分:0)

在使用persist()方法保存时不传递Id(pk),或使用save()方法代替persist()。

答案 2 :(得分:0)

经过大量研究,我发现我拥有的H2 dB没有一个序列表,该表告诉休眠状态它应该计数多少个值,以及如何跟踪插入的值以及接下来要插入的值。我创建了一个序列sql脚本,用于插入名为hst_series的information.sequence并进行相应注释。