JPA gets values ​​from collections of objects

I am new to JPA and now run into a problem!

I have two tables Person and PersonAddress .

In the Person object , I have

@OneToMany(mappedBy="personid")
private Set<Personaddress> personaddressCollection;

and

public Set<Personaddress> getPersonaddressCollection() {
    return this.personaddressCollection;
}

public void setPersonaddressCollection(Set<Personaddress> personaddressCollection) {
    this.personaddressCollection = personaddressCollection;
}

In the PersonAddress object , I have

@ManyToOne
@JoinColumn(name="PERSONID")
private Person personid;

I am making a request similar to the one below:

List<Person> personlist = em.createQuery("SELECT e FROM Person e").getResultList();

I expect it to return all the data from the Person table along with the data in the PersonAddress table in the set available in the Person object. The personlist size is correct, but when I try to read the PersonAddress collection, I get null values. But the database has values ​​in it, and it cannot be null.

PartnerAddress, . JPA, , , ?

, .

+3
1

, Person PersonAddress , Person.

A OneToMany LAZY, SELECT e FROM Person e .

, :

@OneToMany(mappedBy="personid", fetch=FetchType.EAGER)
private Set<Personaddress> personaddressCollection;

FETCH JOIN:

SELECT e FROM Person e LEFT JOIN FETCH e.personaddressCollection

- , . "".

person , PersonAddress, . , .

- "" . , SQL-, , . - .

  • JPA 1.0
    • 9.1.24 "OneToMany Annotation"
    • 4.4.5.3 " "
+3

Source: https://habr.com/ru/post/1761579/


All Articles