Skip to main content
added 439 characters in body
Source Link
BarrySW19
  • 179
  • 1
  • 1
  • 6

Problem: A Hibernate bi-directional One-to-Many relationship does not map easily to JSON. If default (Jackson) mapping is used there is an infinite recursion issue as the parent contains the children each of which contains a reference to the parent. The easiest way I've found to fix this is to mark the Many end attribute as @JsonIgnore. This means that the JSON gets generated as expected. However, when the objects are sent over a REST POST (for example) this means the ManyToOne references get lost. At present I just have the REST service reconstruct the references before persisting the data, but I'm wondering if there is a recommended way to do this automatically (using the Spring REST architecture).

To illustrate, here are some code snippets:

The parent Entity:

@Entity @Table(name = "wine_case") public class WineCase { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "wineCase") private List<CaseContent> caseContents; } 

The child Entity:

@Entity @Table(name = "case_content") public class CaseContent { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "wine_case_id", nullable = false) @JsonIgnore private WineCase wineCase; } 

The code currently needed to servicemethod in the POSTREST controller which receives the request, which currently hasand needs to reconstructrebuild the linkrelationships back from CaseContent to WineCase:

@RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public int@ResponseBody WineCase saveWineCasecreateWineCase(@RequestBody WineCase wineCase, HttpServletResponse response) { wineCase.setId(null); wineCase.getCaseContents().forEach((cc) -> cc.setWineCase(wineCase)); entityManager.persist(wineCase); returnresponse.setHeader("Location", "/winecases/" + wineCase.getId()); return wineCase; } 

Problem: A Hibernate bi-directional One-to-Many relationship does not map easily to JSON. If default (Jackson) mapping is used there is an infinite recursion issue as the parent contains the children each of which contains a reference to the parent. The easiest way I've found to fix this is to mark the Many end attribute as @JsonIgnore. This means that the JSON gets generated as expected. However, when the objects are sent over a REST POST (for example) this means the ManyToOne references get lost. At present I just have the REST service reconstruct the references before persisting the data, but I'm wondering if there is a recommended way to do this automatically (using the Spring REST architecture).

To illustrate, here are some code snippets:

The parent Entity:

@Entity @Table(name = "wine_case") public class WineCase { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "wineCase") private List<CaseContent> caseContents; } 

The child Entity:

@Entity @Table(name = "case_content") public class CaseContent { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "wine_case_id", nullable = false) @JsonIgnore private WineCase wineCase; } 

The code currently needed to service the POST request, which currently has to reconstruct the link from CaseContent to WineCase:

public int saveWineCase(WineCase wineCase) { wineCase.setId(null); wineCase.getCaseContents().forEach((cc) -> cc.setWineCase(wineCase)); entityManager.persist(wineCase); return wineCase.getId(); } 

Problem: A Hibernate bi-directional One-to-Many relationship does not map easily to JSON. If default (Jackson) mapping is used there is an infinite recursion issue as the parent contains the children each of which contains a reference to the parent. The easiest way I've found to fix this is to mark the Many end attribute as @JsonIgnore. This means that the JSON gets generated as expected. However, when the objects are sent over a REST POST (for example) this means the ManyToOne references get lost. At present I just have the REST service reconstruct the references before persisting the data, but I'm wondering if there is a recommended way to do this automatically (using the Spring REST architecture).

To illustrate, here are some code snippets:

The parent Entity:

@Entity @Table(name = "wine_case") public class WineCase { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "wineCase") private List<CaseContent> caseContents; } 

The child Entity:

@Entity @Table(name = "case_content") public class CaseContent { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "wine_case_id", nullable = false) @JsonIgnore private WineCase wineCase; } 

The method in the REST controller which receives the request, and needs to rebuild the relationships back from CaseContent to WineCase:

@RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) public @ResponseBody WineCase createWineCase(@RequestBody WineCase wineCase, HttpServletResponse response) { wineCase.setId(null); wineCase.getCaseContents().forEach((cc) -> cc.setWineCase(wineCase)); entityManager.persist(wineCase); response.setHeader("Location", "/winecases/" + wineCase.getId()); return wineCase; } 
Source Link
BarrySW19
  • 179
  • 1
  • 1
  • 6

Best way to deal with Hibernate 1->Many relationship over REST/JSON service

Problem: A Hibernate bi-directional One-to-Many relationship does not map easily to JSON. If default (Jackson) mapping is used there is an infinite recursion issue as the parent contains the children each of which contains a reference to the parent. The easiest way I've found to fix this is to mark the Many end attribute as @JsonIgnore. This means that the JSON gets generated as expected. However, when the objects are sent over a REST POST (for example) this means the ManyToOne references get lost. At present I just have the REST service reconstruct the references before persisting the data, but I'm wondering if there is a recommended way to do this automatically (using the Spring REST architecture).

To illustrate, here are some code snippets:

The parent Entity:

@Entity @Table(name = "wine_case") public class WineCase { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "wineCase") private List<CaseContent> caseContents; } 

The child Entity:

@Entity @Table(name = "case_content") public class CaseContent { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "wine_case_id", nullable = false) @JsonIgnore private WineCase wineCase; } 

The code currently needed to service the POST request, which currently has to reconstruct the link from CaseContent to WineCase:

public int saveWineCase(WineCase wineCase) { wineCase.setId(null); wineCase.getCaseContents().forEach((cc) -> cc.setWineCase(wineCase)); entityManager.persist(wineCase); return wineCase.getId(); }