下面列出了怎么用org.hibernate.transform.AliasToBeanResultTransformer的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testNativeQueryResultTransformer() {
doInJPA(entityManager -> {
List<BookDTO> books = entityManager.createNativeQuery(
"SELECT " +
" b.id as id, " +
" b.properties as properties " +
"FROM book b")
.unwrap(NativeQuery.class)
.setResultTransformer(new AliasToBeanResultTransformer(BookDTO.class))
.getResultList();
assertEquals(1, books.size());
BookDTO book = books.get(0);
assertEquals(expectedPrice(), book.getProperties().get("price").asText());
});
}
private PostWithXminAndXmax getPost(EntityManager entityManager, Integer id) {
List<PostWithXminAndXmax> result = (List<PostWithXminAndXmax>) entityManager.createNativeQuery(
"SELECT " +
" id, title, CAST(xmin AS text), CAST(xmax AS text) " +
"FROM Post " +
"WHERE id = :id")
.setParameter("id", id)
.unwrap(Query.class)
.setResultTransformer(new AliasToBeanResultTransformer(PostWithXminAndXmax.class))
.getResultList();
return !result.isEmpty() ? result.get(0) : null;
}