playframework org.hibernate.PersistentObjectException:分离的实体传递给坚持:模型

Posted

技术标签:

【中文标题】playframework org.hibernate.PersistentObjectException:分离的实体传递给坚持:模型【英文标题】:playframework org.hibernate.PersistentObjectException: detached entity passed to persist: models 【发布时间】:2013-12-11 11:38:05 【问题描述】:

我正在尝试使用带有 JPA 和 H2 数据库的 playframework 2.2 设置应用程序。 我和这个一起去了: http://www.playframework.com/documentation/2.2.x/JavaJPA 和 computer-database-jpa 示例项目

我遇到了一个错误:

 org.hibernate.PersistentObjectException: detached entity passed to persist: models.

当我运行我的项目时,我收到了这个错误:


堆栈跟踪


        --- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8081

(Server started, use Ctrl+D to stop and go back to the console...)

[info] play - datasource [jdbc:h2:mem:SupraCIRS] bound to JNDI as DefaultDS
[info] play - database [default] connected at jdbc:h2:mem:SupraCIRS
[info] play - Application started (Dev)
[error] play - Cannot invoke the action, eventually got an error: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: models.MedicalIncident
[error] application - 

! @6gh420nj2 - Internal server error, for (POST) [/incydent_medyczny/zapisz] ->

play.api.Application$$anon$1: Execution exception[[PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: models.MedicalIncident]]
        at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.1]
        at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
        at scala.Option.map(Option.scala:145) [scala-library.jar:na]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$2.applyOrElse(PlayDefaultUpstreamHandler.scala:261) [play_2.10.jar:2.2.1]
Caused by: javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: models.MedicalIncident
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1316) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:881) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at models.MedicalIncident.toDataBase(MedicalIncident.java:40) ~[na:na]
        at controllers.MedicalIncidents.submit(MedicalIncidents.java:41) ~[na:na]
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: models.MedicalIncident
        at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:853) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:827) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:831) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:875) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
-

-- (Running the application from SBT, auto-reloading is enabled) ---

[info] play - Listening for HTTP on /0:0:0:0:0:0:0:0:8081

(Server started, use Ctrl+D to stop and go back to the console...)

[info] Compiling 1 Java source to /home/daniel/play-2.2.1/SupraCIRS/target/scala-2.10/classes...

[info] Compiling 3 Scala sources and 1 Java source to /home/daniel/play-2.2.1/SupraCIRS/target/scala-2.10/classes...
[info] play - datasource [jdbc:h2:mem:play] bound to JNDI as DefaultDS
[info] play - database [default] connected at jdbc:h2:mem:play
[error] application - 

! @6gh36abca - Internal server error, for (GET) [/] ->

play.api.UnexpectedException: Unexpected exception[PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory]
        at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:148) ~[play_2.10.jar:2.2.1]
        at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.1]
        at scala.Option.map(Option.scala:145) ~[scala-library.jar:na]
        at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:112) ~[play_2.10.jar:2.2.1]
        at play.core.ReloadableApplication$$anonfun$get$1$$anonfun$apply$1.apply(ApplicationProvider.scala:110) ~[play_2.10.jar:2.2.1]
        at scala.util.Success.flatMap(Try.scala:200) ~[scala-library.jar:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: defaultPersistenceUnit] Unable to build EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:924) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:59) ~[hibernate-entitymanager-4.2.6.Final.jar:4.2.6.Final]
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) ~[hibernate-jpa-2.0-api.jar:1.0.1.Final]
        at play.db.jpa.JPAPlugin.onStart(JPAPlugin.java:35) ~[play-java-jpa_2.10.jar:2.2.1]
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: models.MedicalIncident
        at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:277) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:224) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:665) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3533) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3487) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1376) ~[hibernate-core-4.2.6.Final.jar:4.2.6.Final]


    //end

这是在控制器中处理表单提交的类

    /**
 * Handle the form submission.
 */
@Transactional
public static Result submit() 
    Form<MedicalIncident> filledForm = MedicalIncidentForm.bindFromRequest();

    if(filledForm.hasErrors()) 
        return badRequest(form.render(filledForm));
     else 
        MedicalIncident created = filledForm.get();
        //return ok(confirmation.render("Twój incydent został zgłoszony poprawnie."));
        created.save();
        return ok(summary.render(created));
    

这是我的模型类

    package models;

import java.util.*;
import javax.persistence.*;
import javax.persistence.Entity;
import javax.validation.*;
import play.data.validation.Constraints.*;
import play.api.db.*;
import play.db.jpa.*;
import javax.persistence.EntityManager;

@Entity
public class MedicalIncident   

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public Long id;

    @Required
    public String month;

    @Required
    public String place;

    @Required
    public String unit;

    @Required
    public String incident_type;

    @Required
    public int age;



    /**
     * Insert this new incident submission.
     */
    public void toDataBase() 
        JPA.em().persist(this);
    




请帮帮我

【问题讨论】:

我已经从模型中的 MedicalIncident 类中删除了列表。并且错误不再出现。现在提交表单时出现此错误 [PersistenceException: org.hibernate.PersistentObjectException: detached entity pass to persist: models.MedicalIncident] 【参考方案1】:

从堆栈跟踪中,我认为您没有在 MedicalIncident 类的任何字段上添加 @Id 注释,但由于您没有发布它,所以我不能确定。

Hibernate 需要一个 @Id 字段来唯一标识一条记录。

See hibernate docs.

【讨论】:

我已经发布了整个班级。我希望现在会更容易。我添加了@id 注释。但还是一样 它仍然是相同的堆栈跟踪还是完全改变了?

以上是关于playframework org.hibernate.PersistentObjectException:分离的实体传递给坚持:模型的主要内容,如果未能解决你的问题,请参考以下文章

休眠sql错误1146

Playframework 2.4 单例,对象

如何跟踪 Playframework 调用的所有 HTTP 请求?

手动调用 PlayFramework 自定义错误页面

playframework,是不是有 Validation.clearAfterRenderGet

在 Eclipse 中调试 Playframework