hibernate left Association query returns custom VO

Requirement: multi table associated query to obtain the required fields in each table

1. Create the UserAccountInfoVo entity class, set the get set method, and the fields are all lowercase. Even if the sql is upper case, the return result is lowercase, which will cause the setter method not to be found

public class UserAccountInfoVo implements Serializable {

	private static final long serialVersionUID = 4187685806810273558L;

	private int userid;
	private int orgid;
	private String username;
	private Date loginexpiredate;
	private String email;

	public UserAccountInfoVo() {
	};

	public UserAccountInfoVo(int userid, int orgid, String username, Date loginexpiredate, String email) {
		super();
		this.userid = userid;
		this.orgid = orgid;
		this.username = username;
		this.setLoginexpiredate(loginexpiredate);
		this.email = email;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public int getOrgid() {
		return orgid;
	}

	public void setOrgid(int orgid) {
		this.orgid = orgid;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Date getLoginexpiredate() {
		return loginexpiredate;
	}

	public void setLoginexpiredate(Date loginexpiredate) {
		this.loginexpiredate = loginexpiredate;
	}

}

2. Through the class of ResultTransformer, AliasToBean, and through the query of sql, the array will be returned. Then hibernate will automatically help us set the corresponding field properties according to the mapping of the data table. The value of the query corresponds to the vo set

String sql = "select u.id as userid,u.organizationid as orgid,u.lastname as username,o.login_expire_date as loginExpireDate,u.email from \""
					+ userTable + "\" u ,\"" + organizationTable
					+ "\" o where u.organizationid = o.id and o.type <> 'Free'";
			Query setResultTransformer = session.createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(UserAccountInfoVo.class));
			List<UserAccountInfoVo> UserAccountInfoList = setResultTransformer.list();

Reference resources: https://blog.csdn.net/woshixuye/article/details/7289286

Tags: SQL Hibernate Session

Posted on Fri, 10 Jan 2020 08:01:38 -0800 by Pezmc