hibernate初学习_连表查询

老项目修整。

1.表

enterprise_grade_sheet e

hibernate初学习_连表查询_第1张图片

user u

hibernate初学习_连表查询_第2张图片

2.连表查询出e时 同时显示企业名称

e.enterprise_id=u.userid

3.建立对应的实体以及映射关系

xxx.hbm.xml

**EnterpriseGradeSheet 对应 e表实体**
public class EnterpriseGradeSheetTestDTO extends EnterpriseGradeSheet implements Serializable {
	private Integer enterpriseGradeSheetId;
	private Integer gradeSheetId;
	private  Integer enterpriseId;
	private String userName;
	...get  set
	
	**有参构造,把要查询的字段全部放进去**
	public EnterpriseGradeSheetTestDTO(Integer enterpriseGradeSheetId, Integer gradeSheetId, Integer enterpriseId, String userName) {
		this.enterpriseGradeSheetId = enterpriseGradeSheetId;
		this.gradeSheetId = gradeSheetId;
		this.enterpriseId = enterpriseId;
		this.userName = userName;
	}

4.dao

public List<EnterpriseGradeSheet> test() {
        String hqlStr = null;
        hqlStr = "select new  cn.com.xxxxx.enterprise.dto.EnterpriseGradeSheetTestDTO(e.enterpriseGradeSheetId,e.gradeSheet.gradeSheetId,e.enterpriseBusiness.enterpriseId,u.loginName)  from EnterpriseGradeSheet e,User u where e.enterpriseBusiness.enterpriseId=u.userId";
        List<EnterpriseGradeSheet> list = this.find(hqlStr, null);
        return list;
**查询出来的是EnterpriseGradeSheet集合,select 字段是用dto接收,使用的时候可以用重写后的子类dto的属性,方法正常调用**
    }

你可能感兴趣的:(hibernate)