Usually we like to encapsulate HQL query results into Pojo objects
Syntax
Select New POJO (Id,name) from POJO;
Unable to locate appropriate constructor on class
This encapsulation requires the POJO class to provide the corresponding constructor, the POJO (id,name) construction method.
However, this exception is often thrown in use: unable to locate appropriate constructor on class.
This exception requires several things to check:
1 parameter constructor is the correct argument type
2 The order of the parameter constructors is consistent with the order in the HQL
3 The number of parameters of the parameter constructor is the same as the number of HQL
4 whether the parameter type of the parameter constructor is timestamp
The 4th one is more complex.
This provides the parameter type of the parameter constructor as a timestamp solution:
Super.gethibernatetemplate (). Find ("Select the new Student (id,name,date) from Student");
Entity class:
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 ();
}
Pay attention to some of the construction methods
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 ());
}
}
Origin: Http://blog.sina.com.cn/staratsky