【Hibernate】Unable to locate appropriate constructor on class原因分析
通常我们喜欢将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原因分析的更多相关文章
- Hibernate异常:Unable to locate appropriate constructor on class
异常信息:org.hibernate.hql.ast.QuerySyntaxException: Unable to locate appropriate constructor on class o ...
- Unable to locate appropriate constructor on class报错
在项目开发中,使用Hibernate里的JPA criteria查询,但是在写完之后使用时,会报错:Unable to locate appropriate constructor on class, ...
- Unable to locate appropriate constructor on class异常
一般出现Unable to locate appropriate constructor on class这个异常,都是实体类的带参数的构造方法和使用查询语句出现偏差,两个地方的代码如下: 一般都是第 ...
- 出现Unable to locate appropriate constructor on class 错误可能的原因
1)参数构造器的参数类型是否正确2)参数构造器的顺序和hql中的顺序是否一致3)参数构造器的参数个数是否和hql中的个数一致4)参数构造器的参数类型是否TimeStamp
- 【转】ubuntu 11.04使用apt-get安装软件时一直提示E:unable to locate package
问题: VMware虚拟机安装了ubuntu 11.04,在使用apt-get安装软件时一直提示E:Unable to locate package. 百度了原因,说是要更新源,使用命令:sudo a ...
- adcfgclone.pl appsTier报错Unable to locate 'linkxlC' utility in path
$ cd /u01/dev/apps/apps_st/comn/clone/bin$ perl adcfgclone.pl appsTier Copyrigh ...
- 错误:E:Unable to locate package ...
安装NFS软件包: sudo apt-get install nfs-common 安装tftp软件: sudo apt-get install tftpd-hpa tftp-hpa 均出现此错误:E ...
- Unable to locate secure storage module异常的解决方案
org.eclipse.equinox.security.storage.StorageException: Unable to locate secure storage module 该异常同样是 ...
- ubuntu 14.04 编译内核出现unable to locate package ncurses-devel 问题的解决
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380146d96864968d4e414c422461614 ...
随机推荐
- sklearn.neural_network.MLPClassifier参数说明
目录 sklearn.neural_network.MLPClassifier sklearn.neural_network.MLPClassifier MLPClassifier(hidden_la ...
- 基于 HTML5 WebGL 的地铁站 3D 可视化系统
前言 工业互联网,物联网,可视化等名词在我们现在信息化的大背景下已经是耳熟能详,日常生活的交通,出行,吃穿等可能都可以用信息化的方式来为我们表达,在传统的可视化监控领域,一般都是基于 Web SCAD ...
- MariaDB第二章:基本增删改查
MariaDB 数据类型 MariaDB数据类型可以分为数字,日期和时间以及字符串值. 使用数据类型的原则:够用就行, 尽量使用范围小的,而不用大的 常用的数据类型 整数:int, bit 小数:de ...
- Ansible第一章:基础认识--小白博客
ansible Ansible:Ansible的核心程序Host Lnventory:记录了每一个由Ansible管理的主机信息,信息包括ssh端口,root帐号密码,ip地址等等.可以通过file来 ...
- 为什么重写了equals() 就要重写hashcode()
规定:1.两个对象相等,则hashcode也一定是相等的:2.两个对象相等,对两个对象分别调用equals()都返回 true:3.两个对象有相同的hashcode,但不一定相等 为什么重写了equa ...
- Apache Tomcat 7 Configuration BIO NIO AIO APR ThreadPool
Apache Tomcat 7 Configuration Reference (7.0.93) - The Executor (thread pool)https://tomcat.apache.o ...
- PowerShell-自定义函数(五)-参数互斥:ParameterSetName
转自:https://blog.51cto.com/38088444/1920978 这一篇我们来讲一下参数的互斥,何谓参数互斥呢.用九胖风格的话说就是互怼,有我没你,有你没我. 例如我们为一个Pin ...
- SpringBoot配置devtools实现热部署
spring为开发者提供了一个名为spring-boot-devtools的模块来使Spring Boot应用支持热部署,提高开发者的开发效率,无需手动重启Spring Boot应用. devtool ...
- Ajax提交表单初接触
<!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...
- vue 使用小结 2019.03
v-bind 中使用函数 :attr = 'num' 如上面的例子,通常 num 是 vue 实例中 data 的值,或者是 computed 对象中的值,我们可以在具体函数中计算,改变相应的变量,以 ...