JPQL - 连接 - MultipleTables和空ResultList

时间:2018-04-30 18:50:00

标签: spring hibernate spring-mvc spring-boot jpa

我在JPQL中通过5个表进行查询,但如果其中一个表为NULL,则整个查询失败,resultList为空,并抛出异常。另一方面,如果所有表都不包含null,则它按预期工作。

如何实现它以返回表不为null的所有对象以及null为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";
    }

1 个答案:

答案 0 :(得分:1)

使用LEFT JOIN代替JOIN。默认情况下,JPQL连接为INNER JOIN