Java Spring jpa ManyToOne findAll不返回具有相同@ManyToOne属性的第二条记录
我正在使用spring jpa,我有以下课程:Java Spring jpa ManyToOne findAll不返回具有相同@ManyToOne属性的第二条记录,java,hibernate,spring-data-jpa,Java,Hibernate,Spring Data Jpa,我正在使用spring jpa,我有以下课程: @Entity @Table(name = "student") @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "permanen
@Entity
@Table(name = "student")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "permanentCode")
public class Student implements Serializable {
@Id
private String permanentCode;
....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="classroom_id")
private Classroom classroom;
另一类是:
@Entity
@Table(name = "classroom")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Classroom implements Serializable {
@Id
private String id;
...
@OneToMany(mappedBy = "classroom", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private Set<Student> students;
我想要的是:
[
{
"permanentCode": "FEST120412001",
"firstName": "TEST",
"lastName": "FEST",
"gender": "Female",
"dateOfBirth": "12-04-2012",
"legalTutors": [],
"classroom": {
"id": "P1A",
"year": 1,
"section": "A",
"cycle": 1,
"campus": "PRIMARY",
"students": [
{
"permanentCode": "TEST211114001",
"firstName": "TEST",
"lastName": "TEST",
"gender": "Male",
"dateOfBirth": "21-11-2014",
"legalTutors": [],
"classroom": "P1A"
},
"FEST120412001"
]
}
},
{
"permanentCode": "TEST211114001",
"firstName": "TEST",
"lastName": "TEST",
"gender": "Male",
"dateOfBirth": "21-11-2014",
"legalTutors": [],
"classroom": {
"id": "P1A",
"year": 1,
"section": "A",
"cycle": 1,
"campus": "PRIMARY",
"students": [
{
"permanentCode": "FEST120211001",
"firstName": "TEST",
"lastName": "FEST",
"gender": "Female",
"dateOfBirth": "12-02-2011",
"legalTutors": [],
"classroom": "P1A"
},
"FEST120412001"
]
}
}
]
有人能告诉我做错了什么,或者如何得到我期望的记录吗
更新:
在学生类的教室属性上添加@JsonIgnoreProperties({“students”})
时,如下所示:
@JsonIgnoreProperties({ "students"})
private Classroom classroom;
我得到了第二条记录的详细信息,但里面的教室只显示教室id,如下所示
[
{
"permanentCode": "FEST120412001",
"firstName": "TEST",
"lastName": "FEST",
"gender": "Female",
"dateOfBirth": "12-04-2012",
"legalTutors": [],
"classroom": {
"id": "P1A",
"year": 1,
"section": "A",
"cycle": 1,
"campus": "PRIMARY"
}
},
{
"permanentCode": "TEST120211001",
"firstName": "TEST",
"lastName": "TEST",
"gender": "Male",
"dateOfBirth": "12-02-2011",
"legalTutors": [],
"classroom": "P1A" // No classroom details shown
}
]
还有什么遗漏吗?这个问题是通过添加
@JsonIgnoreProperties({“students”})
解决的,正如Rono
在他的评论(在这个问题的更新中添加)中所建议的那样,并删除@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
如JB Nizet
在本节中所述,从教室的顶端开始。通过添加@JsonIgnoreProperties({“学生”})
解决了这个问题,正如Rono
在其评论中所建议的那样(在本问题的更新中添加),并删除@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class,property=“id”)
位于教室顶部,如本节中的JB Nizet
所述。使用@JsonIgnoreProperties({“student”})
在学生课堂内的教室使用@JsonIgnoreProperties({“students”})
在学生课堂内的教室
@JsonIgnoreProperties({ "students"})
private Classroom classroom;
[
{
"permanentCode": "FEST120412001",
"firstName": "TEST",
"lastName": "FEST",
"gender": "Female",
"dateOfBirth": "12-04-2012",
"legalTutors": [],
"classroom": {
"id": "P1A",
"year": 1,
"section": "A",
"cycle": 1,
"campus": "PRIMARY"
}
},
{
"permanentCode": "TEST120211001",
"firstName": "TEST",
"lastName": "TEST",
"gender": "Male",
"dateOfBirth": "12-02-2011",
"legalTutors": [],
"classroom": "P1A" // No classroom details shown
}
]