Spring jpa ManyToOne findAll does not return the second record that has the same @ManyToOne attribute

huangapple go评论97阅读模式
英文:

Spring jpa ManyToOne findAll does not return the second record that has the same @ManyToOne attribute

问题

  1. [
  2. {
  3. "permanentCode": "FEST120412001",
  4. "firstName": "TEST",
  5. "lastName": "FEST",
  6. "gender": "Female",
  7. "dateOfBirth": "12-04-2012",
  8. "legalTutors": [],
  9. "classroom": {
  10. "id": "P1A",
  11. "year": 1,
  12. "section": "A",
  13. "cycle": 1,
  14. "campus": "PRIMARY",
  15. "students": [
  16. {
  17. "permanentCode": "TEST211114001",
  18. "firstName": "TEST",
  19. "lastName": "TEST",
  20. "gender": "Male",
  21. "dateOfBirth": "21-11-2014",
  22. "legalTutors": [],
  23. "classroom": "P1A"
  24. },
  25. "FEST120412001"
  26. ]
  27. }
  28. },
  29. {
  30. "permanentCode": "TEST211114001",
  31. "firstName": "TEST",
  32. "lastName": "TEST",
  33. "gender": "Male",
  34. "dateOfBirth": "21-11-2014",
  35. "legalTutors": [],
  36. "classroom": {
  37. "id": "P1A",
  38. "year": 1,
  39. "section": "A",
  40. "cycle": 1,
  41. "campus": "PRIMARY",
  42. "students": [
  43. {
  44. "permanentCode": "FEST120211001",
  45. "firstName": "TEST",
  46. "lastName": "FEST",
  47. "gender": "Female",
  48. "dateOfBirth": "12-02-2011",
  49. "legalTutors": [],
  50. "classroom": "P1A"
  51. },
  52. "FEST120412001"
  53. ]
  54. }
  55. }
  56. ]
英文:

I am using spring jpa and I have the following class:

  1. @Entity
  2. @Table(name = "student")
  3. @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  4. @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "permanentCode")
  5. public class Student implements Serializable {
  6. @Id
  7. private String permanentCode;
  8. ....
  9. @ManyToOne(fetch = FetchType.LAZY)
  10. @JoinColumn(name="classroom_id")
  11. private Classroom classroom;

And the other class is:

  1. @Entity
  2. @Table(name = "classroom")
  3. @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
  4. @JsonIdentityInfo( generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
  5. public class Classroom implements Serializable {
  6. @Id
  7. private String id;
  8. ...
  9. @OneToMany(mappedBy = "classroom", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
  10. private Set<Student> students;

When I try to get all the students in postman from my database with:

  1. public List<Student> getAllStudents() {
  2. return studentRepository.findAll();
  3. }

I get only the permanentCode TEST120211001 of the second record that has the same classroom, without any other details, as shown below:

  1. [
  2. {
  3. "permanentCode": "FEST120412001",
  4. "firstName": "TEST",
  5. "lastName": "FEST",
  6. "gender": "Female",
  7. "dateOfBirth": "12-04-2012",
  8. "legalTutors": [],
  9. "classroom": {
  10. "id": "P1A",
  11. "year": 1,
  12. "section": "A",
  13. "cycle": 1,
  14. "campus": "PRIMARY",
  15. "students": [
  16. {
  17. "permanentCode": "TEST120211001",
  18. "firstName": "TEST",
  19. "lastName": "TEST",
  20. "gender": "Male",
  21. "dateOfBirth": "12-02-2011",
  22. "legalTutors": [],
  23. "classroom": "P1A"
  24. },
  25. "FEST120412001"
  26. ]
  27. }
  28. },
  29. "TEST120211001"
  30. ]

What I want instead is something like:

  1. [
  2. {
  3. "permanentCode": "FEST120412001",
  4. "firstName": "TEST",
  5. "lastName": "FEST",
  6. "gender": "Female",
  7. "dateOfBirth": "12-04-2012",
  8. "legalTutors": [],
  9. "classroom": {
  10. "id": "P1A",
  11. "year": 1,
  12. "section": "A",
  13. "cycle": 1,
  14. "campus": "PRIMARY",
  15. "students": [
  16. {
  17. "permanentCode": "TEST211114001",
  18. "firstName": "TEST",
  19. "lastName": "TEST",
  20. "gender": "Male",
  21. "dateOfBirth": "21-11-2014",
  22. "legalTutors": [],
  23. "classroom": "P1A"
  24. },
  25. "FEST120412001"
  26. ]
  27. }
  28. },
  29. {
  30. "permanentCode": "TEST211114001",
  31. "firstName": "TEST",
  32. "lastName": "TEST",
  33. "gender": "Male",
  34. "dateOfBirth": "21-11-2014",
  35. "legalTutors": [],
  36. "classroom": {
  37. "id": "P1A",
  38. "year": 1,
  39. "section": "A",
  40. "cycle": 1,
  41. "campus": "PRIMARY",
  42. "students": [
  43. {
  44. "permanentCode": "FEST120211001",
  45. "firstName": "TEST",
  46. "lastName": "FEST",
  47. "gender": "Female",
  48. "dateOfBirth": "12-02-2011",
  49. "legalTutors": [],
  50. "classroom": "P1A"
  51. },
  52. "FEST120412001"
  53. ]
  54. }
  55. }
  56. ]

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:

  1. @JsonIgnoreProperties({ "students"})
  2. private Classroom classroom;

I get the second record details but the classroom inside it is showing the classroom id only, as shown below.

  1. [
  2. {
  3. "permanentCode": "FEST120412001",
  4. "firstName": "TEST",
  5. "lastName": "FEST",
  6. "gender": "Female",
  7. "dateOfBirth": "12-04-2012",
  8. "legalTutors": [],
  9. "classroom": {
  10. "id": "P1A",
  11. "year": 1,
  12. "section": "A",
  13. "cycle": 1,
  14. "campus": "PRIMARY"
  15. }
  16. },
  17. {
  18. "permanentCode": "TEST120211001",
  19. "firstName": "TEST",
  20. "lastName": "TEST",
  21. "gender": "Male",
  22. "dateOfBirth": "12-02-2011",
  23. "legalTutors": [],
  24. "classroom": "P1A" // No classroom details shown
  25. }
  26. ]

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.

huangapple
  • 本文由 发表于 2020年8月29日 08:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63642463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定