实体类关联

(1/4)inner join

  1. //inner join
  2. @Test
  3. public void innerJoinTest() {
  4. // 查询ID=1160799039167057920的城市的省份名称
  5. HqlHelper helper = HqlHelper.queryFrom(City.class);
  6. helper.join(HqlHelper.currTable, "province", "p")
  7. .fetchOther("p", "name", "provinceName")
  8. .eq("id", "1160799039167057920");
  9. Record tempRecord = helperService.getRecord(helper);
  10. System.err.println(tempRecord);
  11. }

结果:

  1. select
  2. province1_.name as col_0_0_
  3. from
  4. dodo_city city0_
  5. inner join
  6. dodo_province province1_
  7. on city0_.province_id=province1_.id
  8. where
  9. city0_.id=? limit ?
  10. Record [rawData={provinceName=福建}]

(2/4)left join

测试数据:admin表

ID管理员名称
A1超级管理员
A2码叔
A3报表管理员

测试数据:role表

ID角色名称创建该角色的管理员ID
R1超级角色
R2总监A1
R3经理A2
  1. //left join
  2. @Test
  3. public void leftJoinTest() {
  4. // 角色左关联查询创建该角色的管理员,返回角色名称和管理员名称
  5. // 因为'超级角色'这个角色没有创建人,因为是left join ,所以返回的adminName=null
  6. HqlHelper helper = HqlHelper.queryFrom(Role.class);
  7. helper.fetchOther(HqlHelper.currTable, "name", "roleName")
  8. .leftJoin(HqlHelper.currTable, "admin", "a")
  9. .fetchOther("a", "name", "adminName");
  10. Records tempRecords = helperService.getRecords(helper, false);
  11. System.err.println(tempRecords);
  12. }

结果:

  1. select
  2. admin1_.name as col_0_0_,
  3. role0_.name as col_1_0_
  4. from
  5. dodo_role role0_
  6. left outer join
  7. dodo_admin admin1_
  8. on role0_.admin_id=admin1_.id
  9. Records [rawData=[
  10. {adminName=超级管理员, roleName=总监},
  11. {adminName=码叔, roleName=经理},
  12. {adminName=null, roleName=超级角色}]]

(3/4)right join

测试数据:admin表

ID管理员名称
A1超级管理员
A2码叔
A3报表管理员

测试数据:role表

ID角色名称创建该角色的管理员ID
R1超级角色
R2总监A1
R3经理A2
  1. //right join
  2. @Test
  3. public void rightJoinTest() {
  4. // 角色右关联查询创建该角色的管理员,返回角色名称和管理员名称
  5. // 因为'报表管理员'这个管理员没有创建过角色,因为是right join ,所以返回的roleName=null
  6. HqlHelper helper = HqlHelper.queryFrom(Role.class);
  7. helper.fetchOther(HqlHelper.currTable, "name", "roleName")
  8. .rightJoin(HqlHelper.currTable, "admin", "a")
  9. .fetchOther("a", "name", "adminName");
  10. Records tempRecords = helperService.getRecords(helper, false);
  11. System.err.println(tempRecords);
  12. }

结果:

  1. select
  2. admin1_.name as col_0_0_,
  3. role0_.name as col_1_0_
  4. from
  5. dodo_role role0_
  6. right outer join
  7. dodo_admin admin1_
  8. on role0_.admin_id=admin1_.id
  9. Records [rawData=[
  10. {adminName=超级管理员, roleName=总监},
  11. {adminName=码叔, roleName=经理},
  12. {adminName=报表管理员, roleName=null}]]

(4/4)full join

  1. //full join
  2. @Test
  3. public void fullJoinTest() {
  4. // 查询具有相同名称的省份和城市,取一条记录
  5. HqlHelper helper = HqlHelper.queryFrom(City.class);
  6. helper.join(Province.class, "p")
  7. .fetchOther("p", "name", "provinceName")
  8. .eqProperty(HqlHelper.currTable, "name", "p", "name");
  9. Record tempRecord = helperService.getRecord(helper);
  10. System.err.println(tempRecord);
  11. }

结果:

  1. select
  2. province1_.name as col_0_0_
  3. from
  4. dodo_city city0_ cross
  5. join
  6. dodo_province province1_
  7. where
  8. city0_.name=province1_.name limit ?
  9. Record [rawData={provinceName=香港}]