IllegalArgumentException:为TypedQuery [model.User]指定的类型与查询返回类型[class model.User]不兼容 [英] IllegalArgumentException: Type specified for TypedQuery [model.User] is incompatible with query return type [class model.User]

查看:845
本文介绍了IllegalArgumentException:为TypedQuery [model.User]指定的类型与查询返回类型[class model.User]不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建泛型类型的TypedQuery时遇到了这个间歇性错误。我的方法如下:

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

如果我重新启动服务器,错误不会再出现,取结果。

  play.api.Application $$ anon $ 1:执行异常[[IllegalArgumentException:为TypedQuery [model.User]指定的类型是与查询返回类型不兼容[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]
引起:java.lang.IllegalArgumentException:为TypedQuery [model.User]指定的类型是i与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。最终
]
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]


解决方案

问题是

  TypedQuery< T> typedQuery = em.createQuery(query.toString(),type); 

这里var type是一个Class对象而不是User类,你需要这样做。 / p>

  TypedQuery< T> typedQuery = em.createQuery(query.toString(),Class.forName(type.getSimpleName())); 

这将返回一个Class并且TypedQuery将在运行时变为TypedQuery

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

TypedQuery<T> typedQuery =em.createQuery(query.toString(), type);

Here var type is a Class Object not the User class, you need to do something like this.

TypedQuery<T> typedQuery =em.createQuery(query.toString(), Class.forName(type.getSimpleName()));

this will return a Class and TypedQuery will become TypedQuery in runtime

这篇关于IllegalArgumentException:为TypedQuery [model.User]指定的类型与查询返回类型[class model.User]不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆