通常我们喜欢将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. 深度理解 React Suspense(附源码解析)

    本文介绍与 Suspense 在三种情景下使用方法,并结合源码进行相应解析.欢迎关注个人博客. Code Spliting 在 16.6 版本之前,code-spliting 通常是由第三方库来完成的 ...

  2. codeforces#1132 F. Clear the String(神奇的区间dp)

    题意:给出一个字符串S,|S|<=500.每次操作可以删除一段连续的相同字母的子串.问,最少操作多少次可以把这个字符串变成空串. 分析:刚开始的思路是,把连续的串给删除掉,然后再....贪心.完 ...

  3. Linux scp sudo

    command line - scp to remote server with sudo - Super Userhttps://superuser.com/questions/138893/scp ...

  4. [模板] 次短路 | bzoj1726-[Usaco2006Nov]Roadblocks第二短路

    简介 所谓次短路, 顾名思义, 就是第二短路. :P 1到n的次短路长度必然产生于:1到x的最短路 + edge(x,y) + y到n的最短路 简单证明一下: 设 \(dis(i,j)\) 表示 \( ...

  5. nfs 权限问题

    nfs-server 上面的共享文件的权限修改后 到nfs-client 上面挂载后的权限不能刷新的问题 修改/etc/idmap.conf Domain = local.domain.edu #打开 ...

  6. 2019/04/06 BJ省选模拟DAY1

    今天好惨啊 \(n*m\)的图手抖打成\(n*n\)的板子 挂了70分 否则并列rk20?[雾] hyc好厉害啊阿克 省队预订了啊 T1 众所周知向量a,b相乘形成一个矩陣 已知一个矩阵 问至少要多少 ...

  7. Mac spotlight无法搜索的解决方法

    出现问题: 1. 在打开spotlight快速搜索的时候输入两个字符,后该搜索框自动会消失,很是奇怪重启等操作也没有效果 问题原因: 可能因为之前因为耗电原因关闭的全局搜索的索引,或者由于索引出现错误 ...

  8. jieba 库的使用和好玩的词云

    jieba库的使用: (1)  jieba库是一款优秀的 Python 第三方中文分词库,jieba 支持三种分词模式:精确模式.全模式和搜索引擎模式,下面是三种模式的特点. 精确模式:试图将语句最精 ...

  9. Linux grep \t Tab 键失效

    解决方法 1.'$' 有一个日志文件, 比如说有两列, 中间使用 \t制表符分割了.   则在grep查找的时候 使用: '$'\t''\+  代替我们的\t制表符 - 2.-P grep -P '\ ...

  10. 【C#】判断字符串中是否包含指定字符串,contains与indexof方法效率问题

    #方法一:使用string.Contains方法 string.Contains是大小写敏感的,如果要用该方法来判断一个string是否包含某个关键字keyword,需要把这个string和这个key ...