Spring Boot JPA多对多关系 - Rest Web Service无法获取Get All中的所有子属性

问题描述 投票:0回答:1

我有一个简单的多对多关系:Owner <-> Book <-> Publisher

我注意到一个奇怪的行为:当我得到特定的Book时,PublisherOwner(子实体)信息与所有属性一起被提取。然而,在Get所有Books中,孩子的属性缺失。

当我对特定的GETBook时,我得到了Book及其孩子的所有属性:

cURL -XGET http://localhost:8080/books/isbn/978-0743246264

{
   "id":4,
   "name":"Book 4",
   "isbn":"978-0743246264",
   "publishers":[
      {
         "id":1,
         "name":"Publisher 1",
         "description":"Description - 1"
      }
   ],
   "owners":[
      {
         "id":3,
         "name":"Owner 3"
      }
   ]
}

但是,当我为所有书籍运行GET时,某些元素缺少子属性:

cURL -XGET http://localhost:8080/books

[
   {
      "id":1,
      "name":"Book 1",
      "isbn":"978-0743246261",
      "publishers":[
         {
            "id":1,
            "name":"Publisher 1",
            "description":"Description - 1"
         },
         {
            "id":2,
            "name":"Publisher 2",
            "description":"Description - 2"
         }
      ],
      "owners":[
         {
            "id":1,
            "name":"Owner 1"
         }
      ]
   },
   {
      "id":2,
      "name":"Book 2",
      "isbn":"978-0743246262",
      "publishers":[
         {
            "id":4,
            "name":"Publisher 4",
            "description":"Description - 4"
         },
         1,
         {
            "id":3,
            "name":"Publisher 3",
            "description":"Description - 3"
         }
      ],
      "owners":[
         {
            "id":2,
            "name":"Owner 2"
         },
         {
            "id":3,
            "name":"Owner 3"
         },
         1
      ]
   },
   {
      "id":3,
      "name":"Book 3",
      "isbn":"978-0743246263",
      "publishers":[
         4,
         2
      ],
      "owners":[
         2
      ]
   },
   {
      "id":4,
      "name":"Book 4",
      "isbn":"978-0743246264",
      "publishers":[
         1
      ],
      "owners":[
         3
      ]
   }
]

我已经在GitHub上设置了项目,它已经准备好立即运行和测试:https://github.com/tekpartner/learn-spring-boot-many-2-many

hibernate jpa spring-boot jackson many-to-many
1个回答
1
投票

@JsonIdentityInfoPublisher类中删除Owner注释,因此Jackson将序列化该类的完整版本,而不是使用他们的id作为参考。

© www.soinside.com 2019 - 2024. All rights reserved.