[英]JPA mapping entities with hql column names
I am trying to use a feature like RowMapper which provides me with ResultSet, so that I can set the attributes of my pojo by taking resultSet.getString("column_name")
in JPA. 我正在尝试使用像RowMapper这样的功能,该功能为我提供ResultSet,以便可以通过在JPA中使用
resultSet.getString("column_name")
来设置pojo的属性。
But JPA doesn't seems to provide such a feature. 但是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);
}
So instead of doing approvalDTO.setComment(obj[6])
which is the 6th element of array, can I do something like approvalDTO.setComment(rs.getString("comments"));
因此,我可以做诸如
approvalDTO.setComment(rs.getString("comments"));
事情,而不是做approvalDTO.setComment(obj[6])
的第六个元素approvalDTO.setComment(rs.getString("comments"));
approvalDTO.setComment(obj[6])
approvalDTO.setComment(rs.getString("comments"));
? ?
So if in future my column position change in the query, I will not have to change my DAO code to match the column number. 因此,如果将来在查询中更改列的位置,则无需更改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
With JPA 2.1 you have a great possibility to use SqlResultSetMapping
. 使用JPA 2.1,您很有可能使用
SqlResultSetMapping
。 You can find out more for example here: 您可以在此处找到更多示例:
http://www.thoughts-on-java.org/2015/04/result-set-mapping-constructor.html 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-basics.html
http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html http://www.thoughts-on-java.org/2015/04/result-set-mapping-complex.html
The idea is that instead of doing it as you used to do: 这个想法是,而不是像以前那样做:
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];
});
you can introduce an annotation: 您可以引入一个注释:
@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")}))
and afterwards use the mapping (by specifying mapping name) in your query: 然后在查询中使用映射(通过指定映射名称):
List<Author> results = this.em.createNativeQuery("SELECT a.id as authorId, a.firstName, a.lastName, a.version FROM Author a", "AuthorMapping").getResultList();
I am able to fetch the desired result only with Native query and not with NamedNativeQuery - 我只能使用本机查询而不是NamedNativeQuery来获取所需的结果-
Query rcmApprovalTrailQuery = getEntityManager().createNativeQuery(rcmApprovalSqlString.toString(),"ApprovalMapping");
rcmApprovalTrailQuery.setParameter(1,formInstanceId);
List<ApprovaldetailsTbl> approvalList = rcmApprovalTrailQuery.getResultList();
My native query - 我的本机查询-
String RCM_APPROVAL_DETAILS_SQL = "select * "+
" from ApprovalDetails_TBL ad " +
" where ad.ApprovalDetailsId = ? ";
SqlResultSetMapping - SqlResultSetMapping-
@SqlResultSetMapping(name="ApprovalMapping",
entities=@EntityResult(entityClass=ApprovaldetailsTbl.class
))
Note that you need to map all the column names to the entity field names if you are not using * in select query eg - 请注意,如果未在选择查询中使用*,则需要将所有列名称映射到实体字段名称,例如-
fields = {
@FieldResult(name = "col1", column = "alais1"),
@FieldResult(name = "col2", column = "alais2")})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.