Issue
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 I query husband from database using spring data jpa,it occurs nfinite recursion in the result,seeing the follow picture.What's something wrong while using @OneToOne annotation?Could anybody give me some advice? Or I use the annotation in wrong way.
Solution
This is a known issue, when you have bidirectional relation jackson will try to serialize each reference of one side from the other side so its logical to have infinite recursion.
Solution: There are many solutions to that , you could use @JsonIgnore on one side to avoid serializing the annotated reference hence breaking the 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 also could use @JsonManagedReference/@JsonBackReference, check this link for more info how to use them
This answer has one problem , if you try to serialize wife direction you will not have husband object since the solution was to avoid serializing it.
There is a nice solution to this, its mentioned in this link , the idea is to generate a reference to the parent entity, so if you are serializing husband, you will have husband->wife->[reference to husband instead of husband], all you need to do is to annotate your entities with @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
Answered By - Amer Qarabsa
Answer Checked By - Timothy Miller (JavaFixing Admin)