英文:
Spring jpa ManyToOne findAll does not return the second record that has the same @ManyToOne attribute
问题
[
{
"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"
]
}
}
]
英文:
I am using spring jpa and I have the following class:
@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;
And the other class is:
@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;
When I try to get all the students in postman from my database with:
public List<Student> getAllStudents() {
return studentRepository.findAll();
}
I get only the permanentCode TEST120211001
of the second record that has the same classroom, without any other details, as shown below:
[
{
"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": "TEST120211001",
"firstName": "TEST",
"lastName": "TEST",
"gender": "Male",
"dateOfBirth": "12-02-2011",
"legalTutors": [],
"classroom": "P1A"
},
"FEST120412001"
]
}
},
"TEST120211001"
]
What I want instead is something like:
[
{
"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"
]
}
}
]
Can anyone tell what I am doing wrong or how to get my records as I expect them to be?
UPDATE:
When adding @JsonIgnoreProperties({ "students"})
on classroom Attribute in the student class as follow:
@JsonIgnoreProperties({ "students"})
private Classroom classroom;
I get the second record details but the classroom inside it is showing the classroom id only, as shown below.
[
{
"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
}
]
Is there something else missing?
答案1
得分: 0
问题通过在课堂类的顶部按JB Nizet
在这个问题中的解释,移除@JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
,并且按照Rono
在他的评论中建议的方法添加@JsonIgnoreProperties({ "students"})
来解决(在此问题的更新中添加)。
英文:
The problem was solved by adding @JsonIgnoreProperties({ "students"})
as suggested by Rono
in his comment (added in the update of this question) and by removing @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
from the top of the Classroom class as explained by JB Nizet
in this Question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论