[英]hibernate Error : java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to javax.persistence.Query
i try to devlope a small application with java and hibernate, when i run the test class of this application i get this error message: 我尝试使用java和hibernate开发一个小型应用程序,当我运行该应用程序的测试类时,出现以下错误消息:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: insert into REUNION (TITREREUNION, DATEREUNION) values (?, ?)
Hibernate: insert into REUNION (TITREREUNION, DATEREUNION) values (?, ?)
Hibernate: insert into REUNION (TITREREUNION, DATEREUNION) values (?, ?)
Exception in thread "main" java.lang.ClassCastException: org.hibernate.impl.QueryImpl cannot be cast to javax.persistence.Query
at metier.MetierImpl.getAllReunion(MetierImpl.java:62)
at Test.main(Test.java:21)
as you see the error is class MetierImpl : the method that cause the error is : 如您所见,错误是MetierImpl类:导致错误的方法是:
public Reunion getAllReunion(){
Session session =sessionFactory.getCurrentSession();
session.beginTransaction();
Query req=(Query) session.createQuery("select R from Reunion ");
List<Reunion> list = req.getResultList();
Reunion r=(Reunion) req.getResultList().get(0);
return r;
}
Hibernate Session.createQuery() returns an instance of org.hibernate.Query
. Hibernate Session.createQuery()返回
org.hibernate.Query
的实例。 Your code casts this object to javax.persistence.Query
, but it's not an instance of javax.persistence.Query
. 您的代码将此对象强制转换为
javax.persistence.Query
,但它不是javax.persistence.Query
的实例。 So you get this exception. 因此,您会收到此异常。
Don't make this cast, and import the correct Query class: not javax.persistence.Query
, but org.hibernate.Query
. 不要进行
javax.persistence.Query
,而是导入正确的Query类:不是javax.persistence.Query
,而是org.hibernate.Query
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.