hql投影查询之—— [Ljava.lang.Object; cannot be cast to cn.bdqn.guanMingSys.entity.Notice

简介: hql投影查询之—— [Ljava.lang.Object; cannot be cast to cn.bdqn.guanMingSys.entity.Notice

    最近在做项目中遇到一个简单的问题,但是解决了好久,问题就是投影查询。

先来看看出现的异常:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to cn.bdqn.guanMingSys.entity.Notice

   at cn.bdqn.guanMingSys.dao.impl.NoticeDaoImpl.getNoticeByMaxDate(NoticeDaoImpl.java:138)

   at cn.bdqn.guanMingSys.service.impl.NoticeServiceImpl.getNoticeByMaxDate(NoticeServiceImpl.java:132)

   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

   at java.lang.reflect.Method.invoke(Method.java:606)

   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)

   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)

....................

后面的省略一大推看不懂的东西。。。

很显然,问题的本质是转换失败。看看我写的hql语句:

查询最大的日期和名称,就是因为太懒,知道这是个投影查询,就是不想用,哎。。。。

我先在数据库里面查询了一遍,没问题,就没改这个hql语句,以为可以直接使用!

直到在单元测试里面测试了一遍,出错。。。。。

转换异常嘛!好说,我改:

/**
   * (非 Javadoc)
  * <p>Title: getNoticeByMaxDate</p>
  * <p>Description(描述):最近发布的公告 </p>
  * @return
  * @see cn.bdqn.guanMingSys.dao.INoticeDao#getNoticeByMaxDate()
   */
  @Override
  public Notice getNoticeByMaxDate() {
    Notice notice  = new Notice();
    String hql = "select new Notice (MAX(noticedate),noticename) from Notice";
     Session session = this.getSession();
     List<Object[]> noticeList =   session.createQuery(hql).list();
     for (Object[] obj : noticeList) {
      System.out.println(obj[0]);
      notice = (Notice) obj[0];
    }
    return notice;
  }


我再改:

@Override
  public Notice getNoticeByMaxDate() {
    
    String hql = "select MAX(noticeid) from Notice";
     Session session = this.getSession();
     Notice notice =  (Notice) session.createQuery(hql).uniqueResult();
    return notice;
  }


我继续改:

@Override
  public Notice getNoticeByMaxDate() {
    Notice notice = null;
    String hql = "select MAX(noticeid) from Notice ";
    Session session = this.getSession();
    List<Notice> noticeList = session.createQuery(hql).list();
    if(noticeList.size()!=0){
      notice = noticeList.get(0);
    }
    return notice;
  }

.......

改了大概10几遍。我上翻Google,下翻百度!!!左进Statick Overflow,右逛csdn.然而并没有什么卵用!!!

机智的我就去群成员2000左右的java群里面满怀激动的问了一下此问题。


卧槽,群里人还是该吹牛逼的吹牛逼,改撩妹的撩妹!!!居然不鸟我。。。。。(能不求人的尽量不求人!要想不求其他人,自己先强于其他人!)

看来还是得靠自己喽。上个厕所在说吧!!!还别说,多亏这个上厕所这个bug得以解决。。。在上厕所的路上边走边想这个bug,要不用投影查询试试?还试个啥啊,这tmd就是头应查询啊,赶紧在厕所解决完就回来写!

   于是,我在一次的对我的代码进行了更改!!!

改为:

@Override
  public Notice getNoticeByMaxDate() {
    Notice notice  = new Notice();
    String hql = "select new Notice (noticename,MAX(noticedate)) from Notice";
     Session session = this.getSession();
     List<Notice> noticeList =   session.createQuery(hql).list();
     if(noticeList.size()!=0){
       notice = noticeList.get(0);
     }
    return notice;
  }


激动的去运行了一下,,,果不其然的得到如下结果:

   控制太的错误信息:

[cause=org.hibernate.PropertyNotFoundException: no appropriate constructor in class: cn.bdqn.guanMingSys.entity.Notice]


     对于现在的我来说,虽然报错,,,,但是错误换了也好说,总比之前的错误好(没有一点点解决的余地),出现以上的错误,怪我英文水平不行,那就去谷歌呗,看看谷歌怎么说:


 哦哦,他和我说是缺少个构造函数,现在的hql语句的new里面有两个字段,noticename和noticedate,因为构造函数只有全参的和无参的,所以不对应!!!然后我将信将疑的加了个构造函数,怀着一颗紧张又激动的心去点了一下 它,哥们,行不行就靠你了今晚让我几点睡全靠您老的心情了,网开一面让我早点睡吧,明天我还有课呢。。。。神叨叨了半天程序运行完了!行完了!!完了!!!了!!

打开控制台一看:

哈哈哈哈,可以了!!!重新回头看看hql语句的那。。。。原来是这样的!!!


目录
相关文章
|
5月前
libfreetype.so.6: cannot open shared object file: No such file or directory
libfreetype.so.6: cannot open shared object file: No such file or directory
160 0
|
机器学习/深度学习 Linux
解决ImportError libgmpxx.so.4 cannot open shared object file No such file or directory
解决ImportError libgmpxx.so.4 cannot open shared object file No such file or directory
177 0
解决ImportError libgmpxx.so.4 cannot open shared object file No such file or directory
|
2月前
|
Java
cannot open shared object file: No such file or directory
cannot open shared object file: No such file or directory
24 0
|
4月前
FeignClient【问题】Cannot deserialize value of type``from Object value (token `JsonToken.START_OBJECT`)
FeignClient【问题】Cannot deserialize value of type``from Object value (token `JsonToken.START_OBJECT`)
201 0
|
4月前
|
Java
cannot open shared object file: No such file or directory
cannot open shared object file: No such file or directory
36 0
|
5月前
|
NoSQL MongoDB
mongod: error while loading shared libraries: libcrypto.so.1.1: cannot open shared object file: No s
mongod: error while loading shared libraries: libcrypto.so.1.1: cannot open shared object file: No s
149 0
|
5月前
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
gdalinfo: error while loading shared libraries: libgdal.so.30: cannot open shared object file: No su
|
5月前
|
关系型数据库 MySQL
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such
|
8月前
Cannot assign to read only propertyOhobiect“#<Object>
Cannot assign to read only propertyOhobiect“#<Object>