java - Jackson seemingly not persisting bidirectional relationship managed by Hibernate -
i'm having trouble figuring out why owning side of relationship isn't getting persisted on other side when post json object rest api (using spring , hibernate).
mapped superclass id field:
@mappedsuperclass public class baseentity implements serializable { private static final long serialversionuid = 11538918560302121l; @id @generatedvalue(strategy = generationtype.identity) private int id; .... }
owning class (extends namedentity in turn extends baseentity):
@entity @dynamicupdate @selectbeforeupdate @namedquery(name = "chain.byid", query = "from chain 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>(); .... }
owned class:
@entity @dynamicupdate @selectbeforeupdate @namedquery(name = "campaign.byid", query = "from campaign 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; .... }
the relevant part of 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 request body:
{ "name": "mcdonald's", "campaigns": [ { "name": "summer1" } ] }
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": [] }
i suspect expected behaviour when using @jsonidentityinfo annotation break infinite recursion? however, when try request chain created id field, don't see nested object (campaign) anymore.
get method used retrieve chain object 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; }
json response body method:
{ "id": 1, "name": "mcdonald's", "campaigns": [], "rewards": [], "managers": [], "locations": [] }
as can see, campaigns array in chain object empty.
turns out wasn't managing bidirectional relationship properly. relationship must 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; }
Comments
Post a Comment