通常我们喜欢将hql查询结果封装到POJO对象
syntax:
select new POJO(id,name) from POJO ;

这种封装需要POJO类提供对应构造器,POJO(id,name)构造方法。

但使用中经常会抛这样的异常:Unable to locate appropriate constructor on class。

出现这个异常需要检查以下几种情况:
1)参数构造器的参数类型是否正确
2)参数构造器的顺序和hql中的顺序是否一致
3)参数构造器的参数个数是否和hql中的个数一致
4)参数构造器的参数类型是否TimeStamp

其中第4种情况较为复杂

这里提供参数构造器的参数类型是TimeStamp的解决方法:
super.getHibernateTemplate().find("select new Student(id,name,date) from Student");

 实体类:
public class Student {
private Long id;
private String name;
private String address;
private Timestamp date;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Timestamp getDate() {
return date;
}
public void setDate(Timestamp date) {
this.date = date;
}
public Student() {
super();
}
//注意些处的构造方法
public Student(Long id, String name, Object date) {
this.id=id;
this.name = name;
this.date = stringToTimestamp(date.toString());
} public static Timestamp stringToTimestamp(String dateStr){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
try {
Date date = sdf.parse(dateStr);
date.getTime();
cal.setTime(date);
return new Timestamp(cal.getTimeInMillis());
} catch (ParseException e) {
e.printStackTrace();
} cal.setTime(new Date());
return new Timestamp(cal.getTimeInMillis());
}
}

出处:http://blog.sina.com.cn/s/blog_4ad7c2540102uzkc.html

【Hibernate】Unable to locate appropriate constructor on class原因分析的更多相关文章

  1. Hibernate异常:Unable to locate appropriate constructor on class

    异常信息:org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class o ...

  2. Unable to locate appropriate constructor on class报错

    在项目开发中,使用Hibernate里的JPA criteria查询,但是在写完之后使用时,会报错:Unable to locate appropriate constructor on class, ...

  3. Unable to locate appropriate constructor on class异常

    一般出现Unable to locate appropriate constructor on class这个异常,都是实体类的带参数的构造方法和使用查询语句出现偏差,两个地方的代码如下: 一般都是第 ...

  4. 出现Unable to locate appropriate constructor on class 错误可能的原因

    1)参数构造器的参数类型是否正确2)参数构造器的顺序和hql中的顺序是否一致3)参数构造器的参数个数是否和hql中的个数一致4)参数构造器的参数类型是否TimeStamp

  5. 【转】ubuntu 11.04使用apt-get安装软件时一直提示E:unable to locate package

    问题: VMware虚拟机安装了ubuntu 11.04,在使用apt-get安装软件时一直提示E:Unable to locate package. 百度了原因,说是要更新源,使用命令:sudo a ...

  6. adcfgclone.pl appsTier报错Unable to locate 'linkxlC' utility in path

    $ cd /u01/dev/apps/apps_st/comn/clone/bin$ perl adcfgclone.pl  appsTier                     Copyrigh ...

  7. 错误:E:Unable to locate package ...

    安装NFS软件包: sudo apt-get install nfs-common 安装tftp软件: sudo apt-get install tftpd-hpa tftp-hpa 均出现此错误:E ...

  8. Unable to locate secure storage module异常的解决方案

    org.eclipse.equinox.security.storage.StorageException: Unable to locate secure storage module 该异常同样是 ...

  9. ubuntu 14.04 编译内核出现unable to locate package ncurses-devel 问题的解决

    http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461614 ...

随机推荐

  1. 实现在线预览PDF的几种解决方案

    因客户需要实现PDF的预览处理,在网上找了一些PDF在线预览的解决方案,有的用PDFJS的在线预览方式,有的使用PDFObject的嵌入式显示,有的通过转换JPG/PNG方式实现间接显示的方式,开始是 ...

  2. 面试必问Elasticsearch倒排索引原理

    本文摘抄自我的微信公众号"程序员柯南",欢迎关注!原文阅读 倒排索引是目前搜索引擎公司对搜索引擎最常用的存储方式,也是搜索引擎的核心内容,在搜索引擎的实际应用中,有时需要按照关键字 ...

  3. c#, AOP动态代理实现动态权限控制(一)

    因最近工作需要一个动态的权限配置功能,具体实现逻辑是c#的动态代理功能,废话不多说,直接干货.需求: 用户分为管理员.普通用户 不同用户拥有不同功能权限 用户的权限可配置 新增功能时,不用修改权限配置 ...

  4. 云计算openstack共享组件(3)——消息队列rabbitmq

    队列(MQ)概念: MQ 全称为 Message Queue, 消息队列( MQ ) 是一种应用程序对应用程序的通信方法.应用程序通过读写出入队列的消息(针对应用程序的数据)来通信,而无需专用连接来链 ...

  5. yum设置本地源

    创建本地源的文件要放入yum.repos.d目录下,名字随便取,但是后缀要求是.repo 1创建目录 mkdir -p /mnt/cdrom 2虚拟机挂载光盘 mount /dev/cdrom /mn ...

  6. 一、rollup

    参考:reduxreach-routerrollup-starter-librollup-starter-approller-clicreate-react-library 一.安装 npm inst ...

  7. 转 spring注解式参数校验

    转自: https://blog.csdn.net/jinzhencs/article/details/51682830 转自: https://blog.csdn.net/zalan01408980 ...

  8. EasyTouch5ForSiki学院

    总结: 这里面的一些功能,就可以拿来做移动或者PC的很多功能了,这是一个很有用的插件. 禁用0618错误 EasyTouch4_x的写法: using HedgehogTeam.EasyTouch; ...

  9. StringUtils常用方法+StringUtils详细介绍

    StringUtils常用方法+StringUtils详细介绍   StringUtils用法+StringUtils详细介绍博文来源:http://yijianfengvip.blog.163.co ...

  10. Django_ORM_字段属性

    Django_ORM_字段属性 常用字段 AutoField int自增列,必填参 primary_key=True 默认会自动创建一个列名为id的列 IntegerField 一个整数类型,范围在 ...