I am getting this intermittent error while creating a TypedQuery for Generic type. My method is as:
public List<T> findByEmail(Class type, String email_id){
String query = "SELECT t FROM " + type.getSimpleName() + " t WHERE t.email =:"+email_id;
TypedQuery<T> typedQuery =em.createQuery(query, type);
return (List<T>) typedQuery.getResultList();
}
While if I restart the server, the error doesn't reappear and it correctly fetch result.
play.api.Application$$anon$1: Execution exception[[IllegalArgumentException: Type specified for TypedQuery [model.User] is incompatible with query return type [class model.User]]]
at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.7.jar:2.3.7]
at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.7.jar:2.3.7]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun
$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:
2.3.7]
at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun
$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.7.jar:2.3.7]
at scala.Option.map(Option.scala:145) [scala-library-2.11.1.jar:na]
Caused by: java.lang.IllegalArgumentException: Type specified for TypedQuery [model.User] is incompatible with query return type [class model.User]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.resultClassChecking(AbstractEntityManagerImpl.java:387) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final]
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createQuery(AbstractE
ntityManagerImpl.java:344) ~[hibernate-entitymanager-4.3.6.Final.jar:4.3.6.Final
]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.
0_25]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62) ~[na:1.8.0_25]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43) ~[na:1.8.0_25]
The problem is
Here var type is a Class Object not the User class, you need to do something like this.
this will return a Class and TypedQuery will become TypedQuery in runtime