定制 SQL 查询与 CRUD 操作的深入解析
1. SQL 查询结果映射
在进行 SQL 查询时,结果映射是一个关键环节。例如,有如下查询代码:
"left join BID b on b.ITEM_ID = i.ID " +
"group by u.ID, u.USERNAME, u.FIRSTNAME, u.LASTNAME, " +
"u.ACTIVATED, u.STREET, u.ZIPCODE, u.CITY, " +
"ITEM_ID, ITEM_NAME, ITEM_AUCTIONEND",
"SellerItemSummaryResult"
);
List<Object[]> result = query.getResultList();
for (Object[] tuple : result) {
assertTrue(tuple[0] instanceof User);
assertTrue(tuple[1] instanceof BigInteger);
assertTrue(tuple[2] instanceof ItemSummary);
}
该查询的结果映射如下:
<sql-result-set-mapping name="SellerItemSummaryResult">
<entity-result entity-class="org.jpwh.model.querying.User"/>
<constructo