使用spring jpa

时间:2017-12-11 06:48:54

标签: spring spring-mvc spring-data spring-data-jpa

尝试实现

使用activeYn!= N。

从主表和所有相应的子记录中获取所有数据

这是父实体

@Entity
@Table(name="IR_TB_INCIDENT_HDR")
public class IncidentHdr implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR", sequenceName="IR_SEQ_INCIDENT_ID",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="IR_TB_INCIDENT_HDR_INCIDENTID_GENERATOR")
    @Column(name="INCIDENT_ID")
    private long incidentId;

    @Column(name="PATIENT_ID")
    private Long patientId;

    @OneToMany(cascade = {CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE}, fetch = FetchType.LAZY,  mappedBy="incidentHdr")
    private Set<Attachments> attachments;
    ....

    //other child entities

}

这是子实体

@Entity
@Table(name="IR_TB_ATTACHMENTS")
public class Attachments implements Serializable {

    private Long attachmentId;
    private IncidentHdr incidentHdr;
    private boolean activeYn;
}

这里我们正在进行自定义查询,我们在这里只附加一个条件。

public IncidentHdr findIncidentDetailForId(Long incidentId) throws BusinessException {
        StringBuilder query = null;
        IncidentHdr incidentHdr = null;
        StringBuilder incidentDetailQuery = null;
        Query q = null;
        Map < String, Object > parameters = new HashMap < String, Object > ();
        List < String > criteria = new ArrayList < String > ();
        try {

            incidentDetailQuery = new StringBuilder();
            query = new StringBuilder();
            query.append(ReposJPQL.GET_INCIDENTS_DETAIL);

            criteria.add("inc.incidentId = :incidentId ");
            parameters.put("incidentId", incidentId);


            if (criteria.size() > 0) {
                for (int i = 0; i < criteria.size(); i++) {
                    incidentDetailQuery.append(" AND ");
                    incidentDetailQuery.append(criteria.get(i));
                }
            }

            query.append(incidentDetailQuery);
            q = em.createQuery(query.toString());

            for (Entry < String, Object > entry: parameters.entrySet()) {
                q.setParameter(entry.getKey(), entry.getValue());
            }

            incidentHdr = (IncidentHdr) q.getSingleResult();
        }catch(IllegalArgumentException | IllegalStateException | DataAccessException | EntityNotFoundException  e) {
            logger.error(e.getMessage());
            throw new BusinessException(e);
        }
        return incidentHdr;
    }

ReposJPQL,这里使用activeYn条件定义查询。

public interface ReposJPQL {
        public String GET_INCIDENTS_DETAIL = "SELECT inc "
            + " FROM IncidentHdr inc left join inc.attachments att WHERE att.activeYn <> 'N' ";
    }

即使记录存在,它也会返回&#34; org.springframework.dao.EmptyResultDataAccessException:找不到查询的实体;嵌套异常是javax.persistence.NoResultException:找不到查询的实体&#34; 错误

或者还有其他方法可以达到这个目的吗? @Where(clause = ...)选项是纯粹的hibernate所以不能使用它。

0 个答案:

没有答案