JPA本机查询返回具有多个表的字段的实体 [英] JPA native query to return the entity with fields from multiple tables
本文介绍了JPA本机查询返回具有多个表的字段的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在JPA NativeSql中有一个查询,在那里我做表和联接的联合。我创建了一个包含来自多个表的所有查询字段的实体。所以我不能像往常一样用JPA做@Column@ table。
I have a query in JPA NativeSql, where I do "unions" of tables and joins. I made an entity with all the query fields which are from multiple tables. So I can not do "@Column" "@ table" as usual with JPA.
我怎样才能将查询的给定值设置为我的实体? / p>
How could I set the given values of the query to my entity?
推荐答案
您可以使用 @SqlResultSetMapping
。
You can map the columns returned by your native SQL query to your entity by using @SqlResultSetMapping
.
示例:
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")}
)
可以找到更多例子这里。
这篇关于JPA本机查询返回具有多个表的字段的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文