-----业务场景中经常涉及到联查,jpa的hql语法提供了内连接的查询方式(不支持复杂hql,比如left join ,right join).  上代码了

1.我们要联查房屋和房屋用户中间表,通过房屋id关联 homeInfo是房屋表,homeUser是房屋用户中间表.

homeInfo

 package management.entity.carandhome;

 import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table; import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction; import com.alibaba.fastjson.annotation.JSONField;
import management.entity.paymentfee.LifeFee;
import management.entity.paymentfee.ManageFee; @Entity
@Table(name = "t_ss_w_home")
// 房屋信息表
public class HomeInfo implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; public HomeInfo() {
super();
// TODO Auto-generated constructor stub
} @Id
@GeneratedValue
// 房屋ID
private Long homeID; // 用户id,这个是标识房主的用户id,关联表查询出来的房主不靠谱
private Long userID; // @NotNull
// @Size(min = 3, max = 16)
// 房屋面积
private BigDecimal grossArea; // 实际面积
private BigDecimal realArea; // 户型
private String houseType; // 朝向
private String orientaion; // 入住时间
@JSONField(format = "yyyy-MM-dd")
private Date chechTime; // 楼号
private String homeNo; // 单元号
private String homeUnit; // 房间号
private String homeRoom; // 手机号
private String mobileNo; // 备注
private String homeRemark; // 房屋状态
private String homeState; // 房产性质
private String homeNature; // 楼层
private String homeFloor; // 房屋位置
private String homeLo; public Long getUserID() {
return userID;
} public void setUserID(Long userID) {
this.userID = userID;
}
// @OneToMany(fetch = FetchType.EAGER, mappedBy = "homeInfo")
@NotFound(action = NotFoundAction.IGNORE)
private Set<HomeUser> homeUserList = new HashSet<>(); @OneToMany(mappedBy = "homeInfo", fetch = FetchType.EAGER)
@NotFound(action = NotFoundAction.IGNORE)
private Set<ManageFee> manageFeeList = new HashSet<>(); @OneToMany(mappedBy = "homeInfo", fetch = FetchType.EAGER)
@NotFound(action = NotFoundAction.IGNORE)
private Set<LifeFee> lifeFeeList = new HashSet<>(); @NotFound(action = NotFoundAction.IGNORE)
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "homeID", referencedColumnName = "homeID", insertable = false, updatable = false)
private HouseConfirm houseConfirm; public List<HomeUser> getHomeUserList() {
return new ArrayList<>(homeUserList);
} public void setHomeUserList(Set<HomeUser> homeUserList) {
this.homeUserList = homeUserList;
} public Long getHomeID() {
return homeID;
} public void setHomeID(Long homeID) {
this.homeID = homeID;
} public BigDecimal getGrossArea() {
return grossArea;
} public void setGrossArea(BigDecimal grossArea) {
this.grossArea = grossArea;
} public BigDecimal getRealArea() {
return realArea;
} public void setRealArea(BigDecimal realArea) {
this.realArea = realArea;
} public String getHouseType() {
return houseType;
} public void setHouseType(String houseType) {
this.houseType = houseType;
} public String getOrientaion() {
return orientaion;
} public void setOrientaion(String orientaion) {
this.orientaion = orientaion;
} public Date getChechTime() {
return chechTime;
} public void setChechTime(Date chechTime) {
this.chechTime = chechTime;
} public String getMobileNo() {
return mobileNo;
} public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
} public String getHomeRemark() {
return homeRemark;
} public void setHomeRemark(String homeRemark) {
this.homeRemark = homeRemark;
} public String getHomeState() {
return homeState;
} public void setHomeState(String homeState) {
this.homeState = homeState;
} public String getHomeNature() {
return homeNature;
} public void setHomeNature(String homeNature) {
this.homeNature = homeNature;
} public String toString() {
return "{homeState:" + this.homeState + "}";
} public String getHomeLo() {
return homeLo;
} public void setHomeLo(String homeLo) {
this.homeLo = homeLo;
} public List<ManageFee> getManageFeeList() {
return new ArrayList<>(manageFeeList);
} public void setManageFeeList(Set<ManageFee> manageFeeList) {
this.manageFeeList = manageFeeList;
} public List<LifeFee> getLifeFeeList() {
return new ArrayList<>(lifeFeeList);
} public void setLifeFeeList(Set<LifeFee> lifeFeeList) {
this.lifeFeeList = lifeFeeList;
} public String getHomeNo() {
return homeNo;
} public void setHomeNo(String homeNo) {
this.homeNo = homeNo;
} public String getHomeUnit() {
return homeUnit;
} public void setHomeUnit(String homeUnit) {
this.homeUnit = homeUnit;
} public String getHomeRoom() {
return homeRoom;
} public void setHomeRoom(String homeRoom) {
this.homeRoom = homeRoom;
} public String getHomeFloor() {
return homeFloor;
} public void setHomeFloor(String homeFloor) {
this.homeFloor = homeFloor;
} public HouseConfirm getHouseConfirm() {
return houseConfirm;
} public void setHouseConfirm(HouseConfirm houseConfirm) {
this.houseConfirm = houseConfirm;
} }

homeUser

 package management.entity.carandhome;

 import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction; import javax.persistence.*;
import java.io.Serializable; @Entity
@Table(name = "t_ss_w_home_user")
public class HomeUser implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; @Id
@GeneratedValue
private Long homeUserID; private Long userID; private Long homeID; @JSONField
@JsonIgnore
@ManyToOne
@JoinColumn(name = "homeID", insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
private HomeInfo homeInfo; public HomeUser() {
super();
} public HomeInfo getHomeInfo() {
return homeInfo;
} public void setHomeInfo(HomeInfo homeInfo) {
this.homeInfo = homeInfo;
} public Long getHomeUserID() {
return homeUserID;
} public void setHomeUserID(Long homeUserID) {
this.homeUserID = homeUserID;
} public Long getUserID() {
return userID;
} public void setUserID(Long userID) {
this.userID = userID;
} public Long getHomeID() {
return homeID;
} public void setHomeID(Long homeID) {
this.homeID = homeID;
} @Override
public String toString() {
return "HomeUser [homeUserID=" + homeUserID + ", userID=" + userID + ", homeID=" + homeID + "]";
} }

自定义的联查结果实体类

 package management.entity.carandhome;

 import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table; import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction; import com.alibaba.fastjson.annotation.JSONField;
import management.entity.paymentfee.LifeFee;
import management.entity.paymentfee.ManageFee; public class HomeUserAppVO implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; public HomeUserAppVO() {
super();
}
// 这个构造方法必须有, 且要与后面的联查hql语句对应
public HomeUserAppVO(Long homeID, String homeNo, String homeUnit, String homeRoom, String mobileNo,
String homeState, String homeNature, String homeFloor, String homeLo) {
super();
this.homeID = homeID;
this.homeNo = homeNo;
this.homeUnit = homeUnit;
this.homeRoom = homeRoom;
this.mobileNo = mobileNo;
this.homeState = homeState;
this.homeNature = homeNature;
this.homeFloor = homeFloor;
this.homeLo = homeLo;
}
private Long homeID; // 楼号
private String homeNo; // 单元号
private String homeUnit; // 房间号
private String homeRoom; // 手机号
private String mobileNo; // 房屋状态
private String homeState; // 房产性质
private String homeNature; // 楼层
private String homeFloor; // 房屋位置
private String homeLo;
// 用户角色(房主,家人,租客)
private String familyRelationship;
public Long getHomeID() {
return homeID;
}
public void setHomeID(Long homeID) {
this.homeID = homeID;
}
public String getHomeNo() {
return homeNo;
}
public void setHomeNo(String homeNo) {
this.homeNo = homeNo;
}
public String getHomeUnit() {
return homeUnit;
}
public void setHomeUnit(String homeUnit) {
this.homeUnit = homeUnit;
}
public String getHomeRoom() {
return homeRoom;
}
public void setHomeRoom(String homeRoom) {
this.homeRoom = homeRoom;
}
public String getMobileNo() {
return mobileNo;
}
public void setMobileNo(String mobileNo) {
this.mobileNo = mobileNo;
}
public String getHomeState() {
return homeState;
}
public void setHomeState(String homeState) {
this.homeState = homeState;
}
public String getHomeNature() {
return homeNature;
}
public void setHomeNature(String homeNature) {
this.homeNature = homeNature;
}
public String getHomeFloor() {
return homeFloor;
}
public void setHomeFloor(String homeFloor) {
this.homeFloor = homeFloor;
}
public String getHomeLo() {
return homeLo;
}
public void setHomeLo(String homeLo) {
this.homeLo = homeLo;
}
public String getFamilyRelationship() {
return familyRelationship;
}
public void setFamilyRelationship(String familyRelationship) {
familyRelationship = familyRelationship;
}
@Override
public String toString() {
return "HomeUserAppVO [homeID=" + homeID + ", homeNo=" + homeNo + ", homeUnit=" + homeUnit + ", homeRoom="
+ homeRoom + ", mobileNo=" + mobileNo + ", homeState=" + homeState + ", homeNature=" + homeNature
+ ", homeFloor=" + homeFloor + ", homeLo=" + homeLo + ", FamilyRelationship=" + familyRelationship
+ "]";
} }

2.实体类建好,这里我们直接写持久层代码

package management.dao.carandhome;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import management.dao.BaseDao;
import management.entity.carandhome.HomeInfo;
import management.entity.carandhome.HomeUserAppVO;
import management.vo.carHome.HomeInfoVO; @Repository
public interface HomeInfoDao extends BaseDao<HomeInfo> {  //联查,返回自定义对象,使用hql语句. 这里需要注意三点【1.new 全类路径名(属性1,属性2) 2.属性名称要与实体类一一对应,大小写都不能差,否则会报异常 3.自定义类的构造方法里的字段要与这里一一对应】
@Query(
value = "select new management.entity.carandhome.HomeUserAppVO(h.homeID,h.homeNo,h.homeUnit,h.homeRoom,"
+ "h.mobileNo,h.homeState,h.homeNature,h.homeFloor,h.homeLo) from HomeInfo h , HomeUser hu where h.homeID=hu.homeID"
+ " and hu.userID=:id")
List<HomeUserAppVO> findHomeInfoByUserId(@Param("id") Long id); // 原生sql联查语句写法,但是返回对象是一个object【】,因为使用了原生语句,只能自己再去一一对应,然后封装到自己的业务bean中。
@Query(
value = "SELECT " + " h.*, t.countUser " + "FROM " + " ( " + " SELECT "
+ " hu.HomeID homeID, " + " count(hu.UserID) countUser " + " FROM " + " ( "
+ " SELECT " + " hu.HomeID homeID " + " FROM " + " t_ss_w_home_user hu "
+ " WHERE " + " hu.UserID = :userID " + " ) t "
+ " LEFT JOIN t_ss_w_home_user hu ON hu.HomeID = t.homeID " + " GROUP BY "
+ " hu.HomeID " + " ) t " + "LEFT JOIN t_ss_w_home h ON t.homeID = h.HomeID",
nativeQuery = true)
List<Object[]> findByUserIdAndCountUser(@Param("userID") Long userID); }

4.再写原生的sql时,别名 如果不能使用,需要在连接mysql配置时 增加  useOldAliasMetadataBehavior=true(有此问题的需要加,没有的就不用,我的版本不用,这里顺带提一下)

url: jdbc:mysql://mysql:3306/property_manager?useUnicode=true&characterEncoding=utf-8&useOldAliasMetadataBehavior=true

jpa 联表查询 返回自定义对象 hql语法 原生sql 语法 1.11.9版本的更多相关文章

  1. 关于Spring Data JPA 多表查询 返回自定义Vo的问题记录

    这两天开了一个新项目,使用SpringBoot+SpringData,  刚做了一个小功能,都是一张表的操作没什么问题,今天设计到了两张表联查,两张表各取了几个字段,组合成了一个vo, 当我用原生sq ...

  2. Spring Hibernate JPA 联表查询 复杂查询(转)

    今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...

  3. Spring Hibernate JPA 联表查询 复杂查询

    今天刷网,才发现: 1)如果想用hibernate注解,是不是一定会用到jpa的? 是.如果hibernate认为jpa的注解够用,就直接用.否则会弄一个自己的出来作为补充. 2)jpa和hibern ...

  4. Spring Data Jpa 查询返回自定义对象

    转载请注明出处:http://www.wangyongkui.com/java-jpa-query. 今天使用Jpa遇到一个问题,发现查询多个字段时返回对象不能自动转换成自定义对象.代码如下: //U ...

  5. MyBatis联表查询

    MyBatis逆向工程主要用于单表操作,那么需要进行联表操作时,往往需要我们自己去写sql语句. 写sql语句之前,我们先修改一下实体类 Course.java: public class Cours ...

  6. mybatis 联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  7. FreeSql (十七)联表查询

    FreeSql在查询数据下足了功能,链式查询语法.多表查询.表达式函数支持得非常到位. IFreeSql fsql = new FreeSql.FreeSqlBuilder() .UseConnect ...

  8. MyBatis-Plus联表查询的短板,终于有一款工具补齐了

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 哈喽大家好啊,我是Hydra. mybatis-plus作为mybatis的增强工具,它的出现极大的简化了开发中的数据库操作,但是长久以来,它的 ...

  9. sql学习笔记(三)—— 联表查询

    上篇写了一些sql查询的知识,这篇接着写一下有关联表查询的知识. 既然是联表查询,那肯定得多个表啊,所以,我们先创建一个教师表,表名为 teacher,并且向表中插入数据. 准备工作: 创建表语句: ...

随机推荐

  1. C语言中const关键字的用法

    关键字const用来定义常量,如果一个变量被const修饰,那么它的值就不能再被改变,我想一定有人有这样的疑问,C语言中不是有#define吗,干嘛还要用const呢,我想事物的存在一定有它自己的道理 ...

  2. Liferay7 BPM门户开发之6: Activiti数据库换为mysql

    第一步: 在mysql中创建数据库名字叫 'activiti' 执行D:\activiti-5.21.0\database\create下的脚本 第二步: 打开=> apache-tomcat/ ...

  3. Linux入门搭建可视化桌面环境小合集virtual box centOS7.10

    常用命令: 关联主机与虚拟机(方便文件共享): # sudo mount -t vboxsf share(主机文件夹名) /usr/share(虚拟机内自创) Linux shell进入root模式: ...

  4. go微服务框架go-micro深度学习 rpc方法调用过程详解

    摘要: 上一篇帖子go微服务框架go-micro深度学习(三) Registry服务的注册和发现详细解释了go-micro是如何做服务注册和发现在,服务端注册server信息,client获取serv ...

  5. [转]idea导入eclipse的web项目

    https://www.cnblogs.com/xiaoBlog2016/archive/2017/05/08/6825014.html 一.导入自己的web项目 步骤:File->New-&g ...

  6. 从零开始学 Web 之 DOM(六)为元素绑定与解绑事件

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... +-------------------------------------------------------- ...

  7. mysql表分区案例

    0.整理表空间碎片optimize table tablename 1.表分区按年分区,季度子分区 alter table key_part partition by range(year(creat ...

  8. Java并发编程笔记之AbstractQueuedSynchronizer源码分析

    为什么要说AbstractQueuedSynchronizer呢? 因为AbstractQueuedSynchronizer是JUC并发包中锁的底层支持,AbstractQueuedSynchroni ...

  9. [转]Entity Framework and slow bulk INSERTs

    本文转自:https://weblog.west-wind.com/posts/2013/Dec/22/Entity-Framework-and-slow-bulk-INSERTs I’ve been ...

  10. HDFS 安全模式的理解

    安全模式是hadoop的一种保护机制,用于保证集群中的数据块的安全性. 当集群启动的时候,会首先进入安全模式.当系统处于安全模式时会检查数据块的完整性.假设我们设置的副本数(即参数dfs.replic ...