SpringBoot JPA多对多关系 - Rest Webservice无法获取Get All中的所有子属性

时间:2018-01-01 20:01:15

标签: hibernate jpa spring-boot jackson many-to-many

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

我注意到一种奇怪的行为:当我得到特定的Book时,将获取PublisherOwner(子实体)信息以及所有属性。但是,在Get所有Book中,子项的属性都丢失了。

当我对特定GET进行Book时,我会获得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

1 个答案:

答案 0 :(得分:1)

PublisherOwner课程中删除@JsonIdentityInfo注释,因此杰克逊将序列化该课程的完整版本,而不是使用他们的id作为参考。