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);
}
}
}
结果:
这样的方式可以不用写那些复杂的一对多多对一的关系了
推荐阅读
-
在oracle 数据库查询的select 查询字段中关联其他表的方法
-
荐 所有关联表查询方式
-
oracle数据库的同义词Synonym与dblink关联,从而可以直接查询远程数据库的表,不用指定用户和@指定dblink名称
-
同一张表中有父子键关联进行查询
-
hibernate多表查询中单表的同一条记录内存共享时弊病的处理
-
hibernate多表查询中单表的同一条记录内存共享时弊病的处理
-
完爆Facebook/GraphQL,APIJSON全方位对比解析(三)-表关联查询
-
Mybatis中一对多关联表查询
-
Mybatis源码分析--关联表查询及延迟加载(一)
-
Mybatis源码分析--关联表查询及延迟加载原理(二)