我有两个简单的类:
Student
和Certificate
,通过双向@OneToOne关系相关。
我的目标是从学生那里检索证书,使用@OneToOne 和@JsonIgnore 效果很好。
我的 Java 课程:
@Entity
@Table(name = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int studentId;
@Column(name = "age")
private int age;
@Column(name = "mean")
private double mean;
@OneToOne(mappedBy="student" ,cascade = CascadeType.ALL)
@JsonIgnore
private Certificate certificate;
}
@Entity
@Table(name = "certificate")
public class Certificate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int certificateId;
@Column(name = "aknowledgment")
private String aknowledgment;
@OneToOne()
@JoinColumn(name="student_id", nullabl e= false)
@JsonIgnore
private Student student;
}
我在这里面临的问题是我想从学生实体中注入的证书中获取学生 ID,如下所示:
{
studentId: 0,
name: 'John',
age: 19,
certificate : {
certificateId: 0,
aknowledgment : 'granted to',
student : {
studentId: 0
}
}
}
如何避免无限递归但在证书对象中获取该学生的 ID? 非常感谢您的帮助