title: JPA getSingleResult - EntityNotfound Exception
body: <p>Hi I am trying to fetch a java entity through JPA getSingleResult but its throwing an exception I could not find in the <a href="http://docs.oracle.com/javaee/6/api/javax/persistence/Query.html#getSingleResult()" rel="nofollow">API specs</a></p>
  
  <p>Method:</p>
  
  <pre><code>public static Object getSingleResultOrNull(Query q){
          try{
              return q.getSingleResult();
          }catch(NoResultException| EntityNotFoundException enfex){
              System.out.println(enfex.getMessage());
              enfex.printStackTrace();
              return null;
          }catch(NonUniqueResultException ex){
              ex.printStackTrace();
              throw ex;
          }
      }
  </code></pre>
  
  <p>Calling code:</p>
  
  <pre><code>tagName = tagName.trim().toLowerCase();
  Tags tag = (Tags) getSingleResultOrNull(namedQuery("Tags.findByName")
                                          .setParameter("name", tagName));
  </code></pre>
  
  <p>Tag class:</p>
  
  <pre><code>@Entity @Table(name = "tags") @XmlRootElement @NamedQueries({
      @NamedQuery(name = "Tags.findByName", query = "SELECT t FROM Tags t WHERE t.name = :name")}) 
  public class Tags implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "id")
      private Integer id;
      @Basic(optional = false)
      @Column(name = "name")
      private String name;
  </code></pre>
  
  <p><strong>Edit :</strong> Adding ProductTags. class</p>
  
  <pre><code>public class ProductTags implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "id")
      private Integer id;
      @JsonIgnore
      @Column(name = "created")
      @Temporal(TemporalType.TIMESTAMP)
      private Date created;
      @JsonIgnore
      @Column(name = "last_updated")
      @Temporal(TemporalType.TIMESTAMP)
      private Date lastUpdated;
      @JsonIgnore
      @Column(name = "last_updated_by")
      private String lastUpdatedBy;
      @JsonIgnore
      @JoinColumn(name = "prod_id", referencedColumnName = "id")
      @ManyToOne(optional = false)
      private Product prod;
      @JoinColumn(name = "tag_id", referencedColumnName = "id")
      @ManyToOne(optional = false)
      private Tags tag;
  </code></pre>
  
  <p>As per API spec for getSingleResult, only the following exceptions should be thrown: </p>
  
  <ul>
  <li><code>NoResultException</code> - if there is no result</li>
  <li><code>NonUniqueResultException</code> - if more than one result</li>
  <li><code>IllegalStateException</code> - if called for a Java Persistence query
  language UPDATE or DELETE statement </li>
  <li><code>QueryTimeoutException</code> - if the
  query execution exceeds the query timeout value set and only the
  statement is rolled back </li>
  <li><code>TransactionRequiredException</code> - if a lock
  mode has been set and there is no transaction</li>
  <li><code>PessimisticLockException</code> - if pessimistic locking fails and the
  transaction is rolled back </li>
  <li><code>LockTimeoutException</code> - if pessimistic
  locking fails and only the statement is rolled back</li>
  <li><code>PersistenceException</code> - if the query execution exceeds the query
  timeout value set and the transaction is rolled back</li>
  </ul>
  
  <p>However, I am getting a strange exception of javax.persistence.EntityNotFoundException: deleted entity passed to persist: [models.ProductTags#]</p>
  
  <p>for a totally (not totally as ProductTags has a foreign key of Tags object, but in context of saving Tags object, I don't see any relation.) unrelated entity.</p>
  
  <blockquote>
    <p>deleted entity passed to persist: [models.ProductTags#]
    javax.persistence.EntityNotFoundException: deleted entity passed to
    persist: [models.ProductTags#]  at
    org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1369)
      at
    org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1315)
      at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:307)
      at
    controllers.PowerController.getSingleResultOrNull(PowerController.java:68)
      at
    controllers.ProductController.updateProductInfo(ProductController.java:282)
      at
    router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:1584)
      at
    router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$4$$anonfun$apply$4.apply(Routes.scala:1584)
      at
    play.core.routing.HandlerInvokerFactory$$anon$4.resultCall(HandlerInvoker.scala:136)
      at
    play.core.routing.HandlerInvokerFactory$JavaActionInvokerFactory$$anon$14$$anon$3$$anon$1.invocation(HandlerInvoker.scala:127)
      at play.core.j.JavaAction$$anon$1.call(JavaAction.scala:70)     at
    play.http.DefaultHttpRequestHandler$1.call(DefaultHttpRequestHandler.java:20)
      at interceptors.ProductAction.call(ProductAction.java:65)   at
    play.db.jpa.TransactionalAction.lambda$call$5(TransactionalAction.java:19)
      at play.db.jpa.TransactionalAction$$Lambda$43/469822182.apply(Unknown
    Source)   at
    play.db.jpa.DefaultJPAApi.withTransaction(DefaultJPAApi.java:136)     at
    play.db.jpa.JPA.withTransaction(JPA.java:159)     at
    play.db.jpa.TransactionalAction.call(TransactionalAction.java:16)     at
    play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94)  at
    play.core.j.JavaAction$$anonfun$7.apply(JavaAction.scala:94)  at
    scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
      at
    scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
      at
    play.core.j.HttpExecutionContext$$anon$2.run(HttpExecutionContext.scala:40)
      at
    play.api.libs.iteratee.Execution$trampoline$.execute(Execution.scala:70)
      at
    play.core.j.HttpExecutionContext.execute(HttpExecutionContext.scala:32)
      at scala.concurrent.impl.Future$.apply(Future.scala:31)     at
    scala.concurrent.Future$.apply(Future.scala:485)  at
    play.core.j.JavaAction.apply(JavaAction.scala:94)     at
    play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105)
      at
    play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4$$anonfun$apply$5.apply(Action.scala:105)
      at play.utils.Threads$.withContextClassLoader(Threads.scala:21)     at
    play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:104)
      at
    play.api.mvc.Action$$anonfun$apply$1$$anonfun$apply$4.apply(Action.scala:103)
      at scala.Option.map(Option.scala:145)   at
    play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:103)  at
    play.api.mvc.Action$$anonfun$apply$1.apply(Action.scala:96)   at
    play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524)
      at
    play.api.libs.iteratee.Iteratee$$anonfun$mapM$1.apply(Iteratee.scala:524)
      at
    play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560)
      at
    play.api.libs.iteratee.Iteratee$$anonfun$flatMapM$1.apply(Iteratee.scala:560)
      at
    play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:537)
      at
    play.api.libs.iteratee.Iteratee$$anonfun$flatMap$1$$anonfun$apply$14.apply(Iteratee.scala:537)
      at
    scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
      at
    scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
      at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:40)    at
    akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:397)
      at
    scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
      at
    scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
      at
    scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
      at
    scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)</p>
  </blockquote>
  
  <p>I am aware of another way to write the getSingleResultOrNull method but I am not able to find the problem in this case. </p>
  
  <p>Any help on this is appreciated. </p>