
上QQ阅读APP看书,第一时间看更新
Result set
The @SqlResultSetMapping annotation lets you declare a result set and invoke it next time through a native query so that you don't need to implement.
Now you can map the results through simple Plain Old Java Objects (POJO) too thanks to the @ConstructorResult annotation. Here's a sample of an entity that declares a result set through simple POJO:
@SqlResultSetMapping(name = "CustomerDetailsResult", classes = {
@ConstructorResult(targetClass = ResultDetails.class, columns = {
@ColumnResult(name = "id", type = Integer.class),
@ColumnResult(name = "title"),
@ColumnResult(name = "orderCount", type = Integer.class),
@ColumnResult(name = "postCount", type = Integer.class)
})
})
@Entity
public class ResultEntity {...}
The @ConstructorResult annotation declares the class POJO that will represent the result set. Here is the constructor of the ResultDetails class:
public ResultDetails(Integer id, String title, Integer orderCount, Integer postCount) {
this.id = id;
this.title = title;
this.orderCount = orderCount;
t a result set after the query. In the previous API, you can map the result set only to entities or columns.
Now you can map the results through simple Plain Old Java Objects (POJO) too thanks to the @ConstructorResult annotation. Here's a sample of an entity that declares a result set through simple POJO:
@SqlResultSetMapping(name = "CustomerDetailsResult", classes = {
@ConstructorResult(targetClass = ResultDetails.class, columns = {
@ColumnResult(name = "id", type = Integer.class),
@ColumnResult(name = "title"),
@ColumnResult(name = "orderCount", type = Integer.class),
@ColumnResult(name = "postCount", type = Integer.class)
})
})
@Entity
public class ResultEntity {...}
The @ConstructorResult annotation declares the class POJO that will represent the result set. Here is the constructor of the ResultDetails class:
public ResultDetails(Integer id, String title, Integer orderCount, Integer postCount) {
this.id = id;
this.title = title;
this.orderCount = orderCount;
this.postCount = postCount;
}
The native query that we execute to generate the result set:
Query query = entityManager.createNativeQuery(
"SELECT c.JBP_ID as id, c.JBP_TITLE as title, f.JBP_POST_COUNT as orderCount, f.JBP_POST_COUNT AS postCount "
+ "FROM JBP_FORUMS_CATEGORIES c, JBP_FORUMS_FORUMS f "
+ "WHERE f.JBP_CATEGORY_ID = c.JBP_ID " + "GROUP BY c.JBP_ID, c.JBP_TITLE", "CustomerDetailsResult");
List<ResultDetails> resultDetails = query.getResultList();
The query will automatically generate a list of ResultDetail specifying the name of the result set CustomerDetailsResult inside.