具有hql列名的JPA映射实体

时间:2015-04-21 12:37:27

标签: java jpa hql

我正在尝试使用像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])之类的事情,而不是做approvalDTO.setComment(rs.getString("comments"));这是数组的第6个元素吗?

因此,如果将来我的列位置在查询中发生更改,我将不必更改我的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

2 个答案:

答案 0 :(得分:0)

使用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();

答案 1 :(得分:0)

我只能使用Native查询而不是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
            ))

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

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