Hibernate取出需要用到的部分字段


hibernate当数据对象有关联的对象时候,查询该数据对象,也会查询到关联的对象数据,在不需要关联数据,仅需要部分字段数据的情况下,hibernate这样查询会耗性能
下面可以添加多个构造函数(仅有部分字段)如下

public class TestDto implements Serializable{
    private Integer id; //ID	
    private String name; //名称
    private String desc; //描述
    private String user; //用户
    private Date createDate; //创建时间
    private FatherDto fdto;//关联对象
public TestDto(){  
/*这是空的构造方法,有了新的构造方法,这个就必须得写上...... 
 */  
}  
  
public TestDto(Long id, String name, String desc){  
    this.id = id;  
    this.name = name;  
    this.desc= desc;  
}  
......//get,set方法自动生成,这里不写了


}


然后再 hql查询的时候
String hql = "select new TestDto(id, name, desc) from TestDto";  


这里查询的就只查询到三个字段的数据,hibernate也不会再去查询关联的FatherDto 对象数据

猜你喜欢

转载自xixian.iteye.com/blog/1631213
今日推荐