简体   繁体   English

将JPA Eclipse链接查询结果映射到POJO

[英]Map JPA Eclipse Link query result to POJO

Considering the following query: 考虑以下查询:

 entityManager.createQuery("SELECT r.firstname, r.lastname, r.address FROM User r", queryResultDTO.class).getResultList;

The problem is that I will have to deal with different queries(selecting different attributes from different entities) and all the results have to be mapped to the same queryResultDTO.class. 问题是我将不得不处理不同的查询(从不同的实体中选择不同的属性),并且所有结果都必须映射到相同的queryResultDTO.class。 The QueryResultDTO is built to contain all the attributes the queries might return. QueryResultDTO构建为包含查询可能返回的所有属性。

Is it posible to make the queries to always return a list of QueryResultDTO where the objects returned will have the selected attributes true and the unselected ones remain as null? 是否可以使查询始终返回QueryResultDTO列表,在该列表中返回的对象将具有选定的属性true,而未选定的属性仍为null?

BTW. BTW。 It is not the right approach to load entities from database using JPA. 使用JPA从数据库加载实体不是正确的方法。 The better approach is Java Generic with JPA that will save your lot of code. 更好的方法是将Java Generic与JPA一起使用,它将节省大量代码。 You need to write a GenericDao class that will responsible for perform CRUD operations. 您需要编写一个GenericDao类,它将负责执行CRUD操作。

There are lot of examples in internet. 互联网上有很多例子。 Please see here and here 在这里这里看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM