我可以使用 NEW
JPQL 运算符将查询结果映射到 DTO
内吗?
我测试了这段代码:
Query query = em.createNativeQuery("SELECT NEW com.sim.dtos.entities.FreeLocation(t1.galaxie, t1.ss, t1.position) FROM ...");
List<FreeLocation> l = query.getResultList();
但我有异常(exception):
[#|2012-12-20T12:9:21.203+0100|WARNING|glassfish3.1.2|javax.enterprise.system.container.web.com.sun.enterprise.web|_ThreadID=79;_ThreadName=Thread-2;|StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception org.postgresql.util.PSQLException: ERREUR: erreur de syntaxe sur ou près de « . » Position : 15
那么我可以在 native 查询中使用 NEW
运算符吗?
最佳答案
根据定义, native 查询是 SQL 查询。它必须包含适用于您的特定数据库的有效 SQL。
查询将返回List<Object[]>
,迭代列表并创建 FreeLocation
的新实例应该很简单。对于每个 Object[]
数组。
关于JPQL NEW 运算符与 native 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13971125/