例如现在有一个Customer对象
public class Customer{
private Integer cid;
private String cname;
private Integer cage;
}
现在说在hibernate中利用HQL查找出了cname一列值,然后将其封装成一个对象,问HQL要怎么写(投影操作)?
解决:1.在Customer实体类中添加一个带参的构造方法
public class Customer{
public Customer(){
super();
}
public Customer(String cname){
super();
this.cname = cname;
}
private Integer cid;
private String cname;
private Integer cage;
}
2.HQL写成这样List<Customer> lists = session.createQuery("select new Customer(cname) form Customer");