title: playframework org.hibernate.PersistentObjectException: detached entity passed to persist: models
body: <p>I am trying to set up application with playframework 2.2 with JPA and H2 database.
  I went with this:
  <a href="http://www.playframework.com/documentation/2.2.x/JavaJPA" rel="nofollow">http://www.playframework.com/documentation/2.2.x/JavaJPA</a>
  and computer-database-jpa sample project</p>
  
  <p>I AM GETTING AN ERROR:</p>
  
  <pre><code> org.hibernate.PersistentObjectException: detached entity passed to persist: models.
  </code></pre>
  
  <p>when I run my project I am getting that error:</p>
  
  <hr>
  
  <blockquote>
    <p>STACK TRACE</p>
  </blockquote>
  
  <hr>
  
  <pre><code>        --- (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] -&gt;
  
  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) [/] -&gt;
  
  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
  </code></pre>
  
  <p>here is class that handle form submission in controller</p>
  
  <pre><code>    /**
   * Handle the form submission.
   */
  @Transactional
  public static Result submit() {
      Form&lt;MedicalIncident&gt; 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));
      }
  }
  </code></pre>
  
  <p>Here is my  model class</p>
  
  <pre><code>    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);
      }
  
  
  
  }
  </code></pre>
  
  <p>Plese help me</p>