Hibernate查询类转换异常

时间:2014-06-16 05:40:36

标签: hibernate

我有两个表Topic和Members.Members包含创建该主题的成员 主题有成员的内部映射

以下HQL查询适用于Hibernate编辑器

    select  m from Topic as t inner join T.Members m where t.title = 'World News'

说到实现的Java方面

    Object o= session.createQuery("select  m.username,m.membergroup from Topic as t inner
              join t.members m  where t.title = '"+topicTitle+"'");
    Members members=(Members)o;

例外:

    INFO: HHH000397: Using ASTQueryTranslatorFactory
    java.lang.ClassCastException: org.hibernate.internal.QueryImpl cannot be cast to 
    phase.util.db.hbm.object.Members
    at phase.testdevel.ScheduleJob.getMemberDetails(ScheduleJob.java:312)
    at phase.testdevel.ScheduleJob.main(ScheduleJob.java:364)

是Hibernate的新手。感谢任何帮助。谢谢

1 个答案:

答案 0 :(得分:0)

session.createQuery为您提供了一个查询 - 对象,您必须调用列表去获取Members的列表

将您的代码更改为:

 Query o= session.createQuery("select  m.username,m.membergroup from Topic as t inner
              join t.members m  where t.title = '"+topicTitle+"'");
    List<Members> members=o.list();
    if (members != null && members.size()>0){
       return members.get(0);
    } else {
       return null;
    }