问题描述
我真的很困惑,列结果集映射如何工作?当我使用列而不是实体时,我映射的是什么?看看这个例子......
I am really confused, how does column resultset mapping work? What am I mapping when I use columns instead of entities? Look at this example...
Query q = em.createNativeQuery(
"SELECT o.id AS order_id, " +
"o.quantity AS order_quantity, " +
"o.item AS order_item, " +
"i.name AS item_name, " +
"FROM Order o, Item i " +
"WHERE (order_quantity > 25) AND (order_item = i.id)",
"OrderResults");
@SqlResultSetMapping(name="OrderResults",
entities={
@EntityResult(entityClass=com.acme.Order.class, fields={
@FieldResult(name="id", column="order_id"),
@FieldResult(name="quantity", column="order_quantity"),
@FieldResult(name="item", column="order_item")})},
columns={
@ColumnResult(name="item_name")}
)
我可以理解他在这里要做的事情,实体结果将是他想要的结果集,字段会尝试将字段映射到别名,这到底是什么专栏结果呢?它看起来不像是映射到任何东西。
I can understand what he is trying to do here, The Entity result will be the result set he wants, fields will try and map the fields to the aliased names, what the hell is column results doing? It doesn't look like it is mapping to anything.
推荐答案
您将结果集中的4个字段映射到2个Java类:第一个类是Order实体,第二个类是(可能)应包含item_namedb字段的字符串。
You map 4 fields from result set to 2 Java classes: first class is Order entity, and the second is (probably) String that shall contain "item_name" db field.
DB: Java
--- ----
order_id ---> \
order_quantity ---> Order entity
order_item ---> /
item_name ---> String
为了阅读查询结果:
for (Object[] record : query.getResultList()) {
Order order = (Order)record[0];
String itemName = (String)record[1];
}
这篇关于SqlResultSetMapping列为和实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!