我有一个要映射到POJO的NativeQuery。
我尝试了以下方法,但是它以无限循环结束。
public synchronized List<FreeIP> getFirstFreeHost() {
String q = "SELECT i.subnet, h.ip_host FROM ip_addresses i join hosts h on i.id = h.ip_addresses_id ORDER BY i.subnet, h.ip_host";
this.entityManager = this.entityManagerFactory.createEntityManager();
Query query = this.entityManager.createNativeQuery(q);
List<Object[]> rows = query.getResultList();
List<FreeIP> result = new ArrayList<>();
for (Object[] row : rows) {
result.add(new FreeIP((String) row[0], (String) row[1]));
}
return result;
}
答案 0 :(得分:0)
看看QLRM:https://github.com/simasch/qlrm
那么您可以做:
JpaResultMapper jpaResultMapper = new JpaResultMapper();
Query q = em.createNativeQuery("SELECT i.subnet, h.ip_host FROM ip_addresses i join hosts h on i.id = h.ip_addresses_id ORDER BY i.subnet, h.ip_host");
List<EmployeeTO> list = jpaResultMapper.list(q, FreeIP.class);
确保FreeIP的构造函数接受查询的所有参数。