hibernate hql多表查询

hql多表查询包括类似原生SQL查询中的union和join多表联合,hql中这两种联合的查询方式相同
这里首先给出两表的实体结构和数据库结构
表users实体如下

@Entity
@Table(name = "users")
public class UserInfo {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private int id;
	
	@Column(name = "t_name")
	private String name;
	
	@Column(name = "password")
	private String password;
	
	public UserInfo() {}
	public UserInfo(String name, String password) {
		this.name = name;
		this.password = password;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return this.id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return this.name;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return this.password;
	}
}

users表结构如下

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| t_name   | varchar(10)  | NO   |     | NULL    |                |
| password | varchar(100) | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

users1表的实体如下

@Entity
@Table(name = "users1")
public class UserInfo1 {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private int id;
	
	@Column(name = "name")
	private String name;
	
	@Column(name = "password")
	private String password;
	
	@Column(name = "ts")
	private LocalDateTime ts;
	
	public UserInfo1() {}
	public UserInfo1(String name, String password, LocalDateTime ts) {
		this.name = name;
		this.password = password;
		this.ts = ts;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId() {
		return this.id;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getName() {
		return this.name;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getPassword() {
		return this.password;
	}
	public void setTs(LocalDateTime ts) {
		this.ts = ts;
	}
	public LocalDateTime getTs() {
		return this.ts;
	}
}

users1表结构如下

+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(10) | NO   |     | NULL    |                |
| password | varchar(19) | NO   |     | NULL    |                |
| ts       | timestamp   | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+

查询逻辑如下

public List queryBothTableByHql() {
		Session session = sessionFactory.openSession();
		Transaction tr = null;
		List list = new ArrayList();
		try {
			tr = session.beginTransaction();
			String hql = "select new com.syk.ormentity.UserInfo1(a.name, b.password,b.ts) from UserInfo a,UserInfo1 b where a.name=b.name";
			Query query = session.createQuery(hql);
			list = query.list();
			tr.commit();
		}
		catch(HibernateException he) {
			if(tr!=null) {
				tr.rollback();
			}
			he.printStackTrace();
		}
		finally {
			session.close();
			return list;
		}
	}

主函数如下

public class MainTest {
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {
		UserInfoService service = new UserInfoService();
		List userList = new ArrayList();
		userList = service.testQuery();
		for(Object item : userList) {
			UserInfo userInfo = (UserInfo) ((Object[]) item)[0];
			UserInfo1 userInfo1 = (UserInfo1) ((Object[]) item)[1];
			System.out.print("userinfoname: "+userInfo.getName());
			System.out.print(" userinfopassword: "+userInfo.getPassword());
			System.out.print("   ");
			System.out.print("userinfo1name: "+userInfo1.getName());
			System.out.print(" userinfo1password: "+userInfo1.getPassword());
			System.out.println(" userinfo1ts: "+userInfo1.getTs());
		}
	}
}

得到结果如下

userinfoname: shanyikun userinfopassword: ff   userinfo1name: shanyikun userinfo1password: psshanyikun userinfo1ts: 2019-04-05T19:20:50

联合查询两表主要语法为 from table1 a, table2 b, 查询属性可以为两表的属性组合,填装类要存在且不必与任何表映射,且填装类要有查询语句中的构造函数,如果要查询两表的所有属性,可以不用另外建立实体类,hql可以写成

from table1 a,table2b

切记前面不可以加select,此时得到的结果list中每一项是一个Object[], Object[]中第一个元素table1所对应的实体类实例,第二个是table2所对应的实体类实例,按from后的顺序来

你可能感兴趣的:(技术)