JPQL - 连接 - MultipleTables 和空 ResultList
Posted
技术标签:
【中文标题】JPQL - 连接 - MultipleTables 和空 ResultList【英文标题】:JPQL - Joins - MultipleTables and empty ResultList 【发布时间】:2018-10-10 21:37:05 【问题描述】:我在 JPQL 中通过 5 个表进行了查询,但如果其中一个表为 NULL,则整个查询失败并且 resultList 为空并引发异常。另一方面,如果所有表都不包含 null,则它按预期工作。
如何实现它以返回所有表不为空的对象和为空的对象为空?所以我会得到 - Object1[], null, null, null 例如......而不仅仅是空的结果列表...... 非常感谢:)
public List<Object[]> getAdditionalInformation(String ppin)
Query query = em.createQuery("SELECT p, pl, r, d, do FROM Patient p JOIN p.placements pl JOIN pl.room r"
+ " JOIN r.department d JOIN d.doctors do where p.pin = :ppin");
query.setParameter("ppin", ppin);
return query.getResultList();
@Transactional
public AdditionalPD getAdditional(String ppin)
List<Object[]> list = hr.getAdditionalInformation(ppin);
AdditionalPD adp = new AdditionalPD();
Patient patient = null;
Placement placement = null;
Room room = null;
Department department = null;
Doctor doctor = null;
for(Object[] object : list)
patient = (Patient) object[0];
placement = (Placement) object[1];
room = (Room) object[2];
department = (Department) object[3];
doctor = (Doctor) object[4];
adp.setPatientFirstName(patient.getFirstName());
adp.setPatientLastName(patient.getLastName());
adp.setAge(countAge(ppin));
adp.setFrom(placement.getFrom());
adp.setTo(placement.getTo());
adp.setRoomName(room.getName());
adp.setDepartmentName(department.getName());
adp.setDoctorFirstName(doctor.getFirstName());
adp.setDoctorLastName(doctor.getLastName());
return adp;
@GetMapping("/additional/pin/ppin")
public String additionalInformation(@PathVariable String ppin, Model model)
AdditionalPD adp = has.getAdditional(ppin);
model.addAttribute("adp", adp);
return "additional";
【问题讨论】:
见What should I do when someone answers my question? 【参考方案1】:使用LEFT JOIN
代替JOIN
。 JPQL 连接默认为INNER JOIN
s。
【讨论】:
以上是关于JPQL - 连接 - MultipleTables 和空 ResultList的主要内容,如果未能解决你的问题,请参考以下文章
JPQL - 连接 - MultipleTables 和空 ResultList