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

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

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.

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:

确定