杰克逊似乎没有坚持由Hibernate管理的双向关系 [英] Jackson seemingly not persisting bidirectional relationship managed by Hibernate

查看:111
本文介绍了杰克逊似乎没有坚持由Hibernate管理的双向关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个JSON对象发布到我的REST API(使用Spring和Hibernate)时,我很难弄清楚为什么关系的拥有方不会在另一方持久存在。

I'm having trouble figuring out why exactly the owning side of a relationship isn't getting persisted on the other side when I POST a JSON object to my REST API (using Spring and Hibernate).

带有 id 字段的映射超类:

@MappedSuperClass
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 11538918560302121L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    ....
    }

拥有类(extends NamedEntity 反过来扩展 BaseEntity ):

Owning class (extends NamedEntity which in turn extends BaseEntity):

@Entity
@DynamicUpdate
@SelectBeforeUpdate
@NamedQuery(name = "Chain.byId", query = "from Chain where id=:id")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope=Chain.class)
public class Chain extends NamedEntity {
    private static final long serialVersionUID = 4727994683438452454L;
    @OneToMany(mappedBy = "chain", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private List<Campaign> campaigns = new ArrayList<Campaign>();
    ....
}

自有类:

@Entity
@DynamicUpdate
@SelectBeforeUpdate
@NamedQuery(name = "Campaign.byId", query = "from Campaign where id=:id")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope=Campaign.class)
public class Campaign extends NamedEntity {
    @ManyToOne
    @JoinColumn(name = "chain_id")
    private Chain chain;
    ....
}

的相关部分RestController

@RequestMapping(value = "new", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
@ResponseBody
public Chain saveChain(@RequestBody Chain chain) {
    chainDAO.saveChain(chain);
    return chain;
}

JSON请求正文:

{  
  "name": "mcdonald's",
  "campaigns": [
      {
          "name": "summer1"
      }
  ]
}

JSON响应正文的相关部分:

Relevant part of JSON response body:

{
  "id": 1,
  "name": "mcdonald's",
  "campaigns": [
    {
      "id": 1,
      "name": "summer1",
      "rewards": [],
      "startDate": null,
      "endDate": null,
      "chain": null,
      "surveys": [],
      "achievements": []
    }
  ],
  "rewards": []
}

我怀疑这实际上是使用@JsonIdentityInfo注释打破无限递归时的预期行为?但是,当我尝试使用 id 字段创建我刚创建的 Chain 时,我看不到嵌套对象( Campaign

I suspect that this is actually the expected behaviour when using the @JsonIdentityInfo annotation to break infinite recursion? However, when I then try to request the Chain that I just created with its id field, I don't see the nested object (Campaign) anymore.

GET方法我用来检索我刚创建的 Chain 对象:

GET method I used to retrieve the Chain object I just created:

@RequestMapping(value = "{id}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Chain getChain(Model model, @PathVariable int id) {
    Chain chain = chainDAO.getChainById(id).get(0);
    return chain;
}

GET方法的JSON响应正文:

JSON response body for GET method:

{
  "id": 1,
  "name": "mcdonald's",
  "campaigns": [],
  "rewards": [],
  "managers": [],
  "locations": []
}

如您所见,此 Chain 对象中的 campaign 数组现在为空。

As you can see, the campaigns array in this Chain object is now empty.

推荐答案

原来我只是没有正确管理双向关系。必须在两端明确设置关系。

Turns out that I just wasn't managing the bidirectional relationship properly. The relationship must be explicitly set on both ends.

@RequestMapping(value = "{id}/campaigns/new", method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
    @ResponseBody
    public Campaign saveCampaignToChain(@RequestBody Campaign campaign, @PathVariable int id) {
        Chain chain = chainDAO.getChainById(id).get(0);
        chain.getCampaigns().add(campaign);
        campaign.setChain(chain);
        chainDAO.saveChain(chain);
        campaignDAO.saveCampaign(campaign);
        return campaign;
    }

这篇关于杰克逊似乎没有坚持由Hibernate管理的双向关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆