hibernate - Spring Data JPA @OneToOne annotation Infinite Recursion Error -


husband.java

package com.example.demo.com.example.domain;  import lombok.data; import lombok.equalsandhashcode; import lombok.noargsconstructor; import lombok.tostring;  import javax.persistence.*;   //@data //@noargsconstructor //@equalsandhashcode //@tostring @entity @table(name = "t_husban") public class husband {     @id     @generatedvalue(strategy = generationtype.auto)     private long id;      private string name;     private string job;      @onetoone     @joincolumn(name = "wife_fk",referencedcolumnname = "id")     private wife wife;      //omitted getter/setter } 

wife.java

package com.example.demo.com.example.domain;  import lombok.data; import lombok.equalsandhashcode; import lombok.noargsconstructor; import lombok.tostring;  import javax.persistence.*;  //@data //@noargsconstructor @equalsandhashcode(exclude = "husband",callsuper = false) @entity @table(name = "t_wife") public class wife {      @id     @generatedvalue(strategy = generationtype.auto)     private long id;      private string name;      @onetoone(mappedby = "wife",cascade = {cascadetype.all})     private husband husband;      //omitted getter/setter } 

service.java

@service      public class testonetooneeitherside {          @autowired         private wiferepository wifedao;          @autowired         private husbandrepository husbanddao;         public husband testcreate() {              husband husband = husbanddao.findbyname("wang");             return husband;         }     } 

when query husband database using spring data jpa,it occurs nfinite recursion in result,seeing follow picture.what's wrong while using @onetoone annotation?could give me advice? or use annotation in wrong way.

the picture

this known issue, when have bidirectional relation jackson try serialize each reference of 1 side other side logical have infinite recursion.

solution: there many solutions , use @jsonignore on 1 side avoid serializing annotated reference hence breaking infinite recursion

 @equalsandhashcode(exclude = "husband",callsuper = false)  @entity @table(name = "t_wife") public class wife {  @id @generatedvalue(strategy = generationtype.auto) private long id;  private string name;  @onetoone(mappedby = "wife",cascade = {cascadetype.all}) @jsonignore private husband husband;  //omitted getter/setter } 

you use @jsonmanagedreference/@jsonbackreference, check link more info how use them

this answer has 1 problem , if try serialize wife direction not have husband object since solution avoid serializing it.

there nice solution this, mentioned in link , idea generate reference parent entity, if serializing husband, have husband->wife->[reference husband instead of husband], need annotate entities @jsonidentityinfo

@equalsandhashcode(exclude = "husband",callsuper = false) @entity @table(name = "t_wife") @jsonidentityinfo(generator=objectidgenerators.uuidgenerator.class, property="@id") public class wife {  @jsonidentityinfo(generator=objectidgenerators.uuidgenerator.class, property="@id") @entity @table(name = "t_husban") public class husband { @id 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -