与Jackson 2的JSON,我需要类似XMLAdapter的功能

时间:2014-05-22 22:16:54

标签: java json spring hibernate jackson

所以我在这个问题上挣扎了一个星期左右。 我以前使用来自Database的XML响应创建了一个服务,我成功加载了所有实体并将它们与XMLAdapter一起使用。

这适用于读取,写入,创建和删除。 特别脆弱的时刻是收藏品,但适配器使它们的工作非常好。

所以我现在正在使用JSON而且我不能让Hibernate Collections正常工作。我要么得到完全递归的急切加载的集合,这在大多数情况下会发送整个数据库,或者在收藏地点:(。

我应该采取什么方法?提前谢谢!


示例回复,列出了所有用户:

[
   {
      "username":"bob",
      "password":"8ee3086749f7fa95ffe9c4588037cb10",
      "enabled":true,
      "email":null,
      "name":null,
      "version":1,
      "authorities":[
         {
            "id":3,
            "version":0,
            "description":null,
            "comment":null,
            "authority":"ROLE_USER"
         },
         {
            "id":2,
            "version":0,
            "description":null,
            "comment":null,
            "authority":"ROLE_ADMIN"
         }
      ],
      "messages":[
         {
            "id":2,
            "version":0,
            "description":null,
            "comment":null,
            "message":"TEST BACK"
         }
      ],
      "meetings":[
         {
            "id":1,
            "version":0,
            "description":null,
            "comment":null,
            "messages":[
               2,
               {
                  "id":1,
                  "version":0,
                  "description":null,
                  "comment":null,
                  "message":"TEST"
               }
            ]
         }
      ]
   },
   {
      "username":"rod",
      "password":"e97673c55fc4f2af8bf2122333df24ac",
      "enabled":true,
      "email":null,
      "name":null,
      "version":1,
      "authorities":[
         {
            "id":1,
            "version":0,
            "description":null,
            "comment":null,
            "authority":"ROLE_USER"
         }
      ],
      "messages":[
         1
      ],
      "meetings":[
         1
      ]
   }
]

所以我不明白...而f.e.当局可能是好的,但后来f.e.会议正在加载其所有的收藏品,并且所有收集都会影响" rob"用户以后,因为entites没有加倍。

我认为是因为我使用@JsonManagedReference@JsonBackReference,但我不知道是否正确。

1 个答案:

答案 0 :(得分:0)

这篇文章很有帮助我理解了这个问题; 仔细阅读!

http://www.javacodegeeks.com/2011/10/avoid-lazy-jpa-collections.html