首页 > 解决方案 > spring boot jpa hibernate - 获取主详细数据

问题描述

我有两张桌子

ORDERS
ORDERITEMS
ORDERS will be having columns - ORDERS_ID, STATUS, TIMEPLACED,FIELD1,FIELD2,LASTUPDATE
ORDERITEMS is having columns - ORDERITEMS_ID, ORDERS_ID,PARTNUM,STATUS,FIELD1,FIELD2.

我的要求是获取以下 JSON 格式的数据

{
   "orders_id":"1234",
    "status":"C",
   "timeplaced":"2018-05-10",
    orderitems:[
         {
              orderitems_id="7",
              orders_id="1234",
              status="C"
         },{
             orderitems_id = 8,
             orders_id = 1234,
             status='C'
         }
    ]
}

这里的 ORDER 详细信息就像我的响应中的标题,而 items 是与标题中存在的订单 ID 相对应的 orderitems 数组。

但是当我在 orderitems 实体上使用 ManyToOne 时,每个 orderitem id 都会重复订单详细信息。但这不是我想要的。我想在父级别获得订单详细信息,在子级别获得项目详细信息。

是否可以扩展 CrudRepository 或者我应该使用 HQL 查询?如果可能的话,你能给我一个方法吗?

标签: hibernatespring-bootjpa

解决方案


推荐阅读