1

I am trying to set up application with playframework 2.2 with JPA and H2 database. I went with this: http://www.playframework.com/documentation/2.2.x/JavaJPA and computer-database-jpa sample project

I AM GETTING AN ERROR:

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

when I run my project I am getting that error:


STACK TRACE


        --- (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

here is class that handle form submission in controller

    /**
 * 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));
    }
}

Here is my model class

    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);
    }



}

Plese help me

1
  • I've removed list from MedicalIncident class in model. ANd error doesnt show up again. Now I have this error when submitting the form [PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: models.MedicalIncident] Commented Dec 11, 2013 at 13:19

1 Answer 1

3

From the stack trace I presume you haven't put an @Id annotation on any fields in the MedicalIncident class but as you haven't posted it I can't be sure.

Hibernate requires an @Id field to uniquely identify a record.

See hibernate docs.

Sign up to request clarification or add additional context in comments.

2 Comments

I've posted whole class. I hope it will be easier now. I've added @id annotation. But still the same
Is it still the same stack trace or has that changed at all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.