HibernateUnable to locate appropriate constructor on class原因分析

临文乍了了,彻卷兀若无。这篇文章主要讲述HibernateUnable 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");

1 实体类: 2 public class Student { 3 private Long id; 4 private String name; 5 private String address; 6 private Timestamp date; 7 public Long getId() { 8return id; 9 } 10 public void setId(Long id) { 11this.id = id; 12 } 13 public String getName() { 14return name; 15 } 16 public void setName(String name) { 17this.name = name; 18 } 19 public String getAddress() { 20return address; 21 } 22 public void setAddress(String address) { 23this.address = address; 24 } 25 public Timestamp getDate() { 26return date; 27 } 28 public void setDate(Timestamp date) { 29this.date = date; 30 } 31 public Student() { 32super(); 33 } 34 //注意些处的构造方法 35 public Student(Long id, String name, Object date) { 36 this.id=id; 37this.name = name; 38this.date = stringToTimestamp(date.toString()); 39 } 40 41 public static Timestamp stringToTimestamp(String dateStr){ 42 43SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 44Calendar cal = Calendar.getInstance(); 45try { 46Date date = sdf.parse(dateStr); 47date.getTime(); 48cal.setTime(date); 49return new Timestamp(cal.getTimeInMillis()); 50} catch (ParseException e) { 51e.printStackTrace(); 52} 53 54cal.setTime(new Date()); 55return new Timestamp(cal.getTimeInMillis()); 56 } 57 }

【HibernateUnable to locate appropriate constructor on class原因分析】出处:http://blog.sina.com.cn/s/blog_4ad7c2540102uzkc.html








    推荐阅读