具有 hql 列名称的 JPA 映射实体

JPA mapping entities with hql column names

我正在尝试使用像 RowMapper 这样的功能,它为我提供了 ResultSet,这样我就可以通过在 JPA 中使用 resultSet.getString("column_name") 来设置我的 pojo 的属性。

但是JPA好像没有提供这样的功能。

StringBuffer rcmApprovalSqlString = new StringBuffer(QueryConstants.APPROVAL_DETAILS_SQL);
List<ApprovalDTO> finalApprovalList = null;

Query rcmApprovalTrailQuery = getEntityManager().createQuery(rcmApprovalSqlString.toString());
rcmApprovalTrailQuery.setParameter(1,formInstanceId);

List<?> approvalList = rcmApprovalTrailQuery.getResultList();
finalApprovalList = new ArrayList<ApprovalDTO>();
for(Object approvalObj : approvalList){
    Object[] obj = (Object[]) approvalObj;

    ApprovalDTO approvalDTO = new ApprovalDTO();
    approvalDTO.setDeptName(obj[0]!=null? obj[0].toString() : NAPSConstants.BLANK_STRING);
    approvalDTO.setUserId(obj[1]!=null? obj[1].toString()+" "+obj[2].toString() : NAPSConstants.BLANK_STRING);

    approvalDTO.setComment(obj[6]!=null? obj[6].toString() : NAPSConstants.BLANK_STRING);

    finalApprovalList.add(approvalDTO);
}

所以我可以不做 approvalDTO.setComment(obj[6]) ,它是数组的第 6 个元素,我可以做类似 approvalDTO.setComment(rs.getString("comments")); 的事情吗?

因此,如果将来我的列位置在查询中发生变化,我将不必更改我的 DAO 代码来匹配列号。

My hql query = select   ad.departmentid.departmentname, ad.userid.userfirstname, ad.userid.userlastname, ad.napsroleid.napsrolename, 
        ad.approvalstatus, ad.approvaltimestamp, ad.approvalcomments 
from    ApprovaldetailsTbl ad 
where   ad.forminstanceid.forminstanceid = ?1 
order by approvallevelid asc

对于 JPA 2.1,您很有可能使用 SqlResultSetMapping。例如,您可以在此处找到更多信息:

http://www.thoughts-on-java.org/2015/04/result-set-mapping-constructor.html

http://www.thoughts-on-java.org/2015/04/result-set-mapping-basics.html

http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html

我们的想法是,不要像以前那样做:

List<Object[]> results = this.em.createNativeQuery("SELECT a.id, a.firstName, a.lastName, a.version FROM Author a").getResultList();

    results.stream().forEach((record) -> {
            Long id = ((BigInteger) record[0]).longValue();
            String firstName = (String) record[1];
            String lastName = (String) record[2];
            Integer version = (Integer) record[3];
    });

可以引入注解:

@SqlResultSetMapping(
        name = "AuthorMapping",
        entities = @EntityResult(
                entityClass = Author.class,
                fields = {
                    @FieldResult(name = "id", column = "authorId"),
                    @FieldResult(name = "firstName", column = "firstName"),
                    @FieldResult(name = "lastName", column = "lastName"),
                    @FieldResult(name = "version", column = "version")}))

然后在您的查询中使用映射(通过指定映射名称):

List<Author> results = this.em.createNativeQuery("SELECT a.id as authorId, a.firstName, a.lastName, a.version FROM Author a", "AuthorMapping").getResultList();

我只能使用本机查询而不是 NamedNativeQuery 获取所需的结果 -

Query rcmApprovalTrailQuery = getEntityManager().createNativeQuery(rcmApprovalSqlString.toString(),"ApprovalMapping");
            rcmApprovalTrailQuery.setParameter(1,formInstanceId);

            List<ApprovaldetailsTbl> approvalList = rcmApprovalTrailQuery.getResultList();

我的原生查询 -

String RCM_APPROVAL_DETAILS_SQL = "select * "+
                " from  ApprovalDetails_TBL ad " +
                " where ad.ApprovalDetailsId = ? ";

SqlResultSetMapping -

@SqlResultSetMapping(name="ApprovalMapping",
        entities=@EntityResult(entityClass=ApprovaldetailsTbl.class
            ))

请注意,如果您不在 select 查询中使用 *,则需要将所有列名称映射到实体字段名称,例如 -

fields = {
                        @FieldResult(name = "col1", column = "alais1"),
                        @FieldResult(name = "col2", column = "alais2")})