Home > database >  How to include all values of OneToMany mapped field
How to include all values of OneToMany mapped field

Time:10-13

EDIT: Better question overall

I have the following two domains:

@Entity
@Table(name="PERSON")
class Person implements Serializable {

    @Id
    @Column(name="person_id")
    Long id

    @Column(name="person_pidm")
    Long pidm

    @Column(name="person_first_name")
    String first_name

    @Column(name="person_last_name")
    String last_name

    @OneToMany(mappedBy="pidm")
    private List<Telephone> phones

    List<Telephone> getPhones() {
        return phones
    }
}

@Entity
@Table(name="TELEPHONE")
class Telephone implements Serializable {

    @Id
    @Column(name="telephone_id")
    Long id

    @Column(name="telephone_phone_number")
    String phone_number

    @ManyToOne
    @JoinColumn(name="telephone_pidm", referencedColumnName="person_pidm")
    private Person pidm;

}

With this, I am able to query a person, and get the following response:

def result = Person.executeQuery(
    "SELECT s "  
    "FROM Person "  
    "WHERE s.pidm = :pidm",
    [pidm: pidm]
)
{
    "id": 186506,
    "first_name": "JANE",
    "last_name": "DOE",
    "pidm": 324950
}

But when needed, I would also like to include the phones field, and get something like this.

{
    "id": 186506,
    "phones": [
        {
            "id": 329815,
            "phone_number": "1234567890"
        },
        {
            "id": 329816,
            "phone_number": "0987654321"
        }
    ],
    "first_name": "JANE",
    "last_name": "DOE",
    "pidm": 324950
}

How would I go about performing the second query to includes the phones field?

CodePudding user response:

I hope this will work out

@OneToMany(cascade = CascadeType.ALL,mappedBy="pidm")
@JsonBackReference
private List<SprteleDetail> phones

and

@ManyToOne
@JoinColumn(name="telephone_pidm", referencedColumnName="person_pidm")
@JsonManagedReference
private Person pidm;

@JsonManagedReferences and JsonBackReferences are used to display objects with parent child relationship. @JsonManagedReferences is used to refer to parent object and @JsonBackReferences is used to mark child objects.

CodePudding user response:

@ManyToOne
@JoinColumn(name="telephone_pidm", referencedColumnName="person_pidm")
@JsonIgnore //try this
private Person pidm;

CodePudding user response:

Add @JsonBackReference on the field you want in the json like

 @ManyToOne
 @JsonBackReference
 @JoinColumn(name="telephone_pidm", referencedColumnName="person_pidm")
 private Person pidm;

CodePudding user response:

try this

@ManyToOne
@JoinColumn(name="telephone_pidm", referencedColumnName="person_pidm")
@JsonBackReference //try this
private Person pidm;

CodePudding user response:

Try adding @JsonBackReference to the second table like below

@Entity
@Table(name="TELEPHONE")
class Telephone implements Serializable {

    @Id
    @Column(name="telephone_id")
    Long id

    @Column(name="telephone_phone_number")
    String phone_number
    @JsonBackReference
    @ManyToOne
    @JoinColumn(name="telephone_pidm", referencedColumnName="person_pidm")
    private Person pidm;

}

Also, in your parent entity the mapping of second table is different

@OneToMany(mappedBy="pidm")
private List<SprteleDetail> phones

It should be

@OneToMany(mappedBy="pidm")
private List<Telephone> phones

If your second entity is named as Telephone

  • Related