欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate两张表关联查询

程序员文章站 2022-04-16 22:47:07
...

很久没用过hibernate了,今天尝试了一下多表连接查询,忘记了怎么配置,然后网上搜到了另一种的联合查询方式。

首先是两个实体类,一个用户表,一个商品表

User类

public class User {
	private Integer id;
	private String userName;
	private String phone;
	private String address;

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", userName=" + userName + ", phone=" + phone + ", address=" + address + "]";
	}
	
}

Goods商品类

public class Goods {
	private Integer goodId;
	private String goodName;
	private Integer userId;
	public Integer getGoodId() {
		return goodId;
	}
	public void setGoodId(Integer goodId) {
		this.goodId = goodId;
	}
	public String getGoodName() {
		return goodName;
	}
	public void setGoodName(String goodName) {
		this.goodName = goodName;
	}
	public Integer getUserId() {
		return userId;
	}
	public void setUserId(Integer userId) {
		this.userId = userId;
	}
	@Override
	public String toString() {
		return "Goods [goodId=" + goodId + ", goodName=" + goodName + ", userId=" + userId + "]";
	}
	
}

这里就不上传hibernate的cfg和hbm的映射了。

这时候想要多表连接可以添加一个第三方的类,把需要的属性都封装到第三个整合的实体类中,并且已构造器的方式传入

UserAndGoods整合类

public class UserAndGoods {
	//以下是我们想要查出来的数据
	private String username;
	private String phone;
	private String address;
	private String goodname;
	
	public UserAndGoods() {
		super();
	}
	//想要封装的数据已构造器的方式传入
	public UserAndGoods(String username, String phone, String address, String goodname) {
		super();
		this.username = username;
		this.phone = phone;
		this.address = address;
		this.goodname = goodname;
	}

	public String getUsername() {
		return username;
	}

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

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getGoodname() {
		return goodname;
	}

	public void setGoodname(String goodname) {
		this.goodname = goodname;
	}

	@Override
	public String toString() {
		return "UserAndGoods [username=" + username + ", phone=" + phone + ", address=" + address + ", goodname="
				+ goodname + "]";
	}
	
	
}

测试类

public class Shop {
	@Test
	public void ss(){
		Session session = HibernateUtils.openSession();
		Transaction transaction = session.beginTransaction();
		//在hql语句中直接new我们写好的整合的类,并且用构造器传入
		String hql ="select new com.hbsi.pojo.UserAndGoods(u.userName,u.phone,u.address,g.goodName) from User u,Goods g  where u.id = g.userId and u.id = 1";
		//集合的泛型是整合类
		List<UserAndGoods> list = session.createQuery(hql).list();
		transaction.commit();
		for (UserAndGoods userAndGoods : list) {
			System.out.println(userAndGoods);
		}
		
	}
}

结果:

hibernate两张表关联查询

 这样的方式可以不用写那些复杂的一对多多对一的关系了