Diego Izquierdo Dussan
Diego Izquierdo Dussan

Reputation: 157

Why jpa return empty in a @OneToMany if there are data in the other table

Hi I have a OneToMany relationchip in a entity. when I do the findAll JPA bring me the data from all the @ManyToOne but bring me [] empty for the @OneToMany (the collection come empty).

This is the how is the relation in the main entity:

public class Emprendimientos implements Serializable {
.
.
.
@OneToMany(cascade = CascadeType.ALL, mappedBy = "logoId")
private Collection<LogosEmprendimientos> logosEmprendimientos;

The code in the other entity:

public class LogosEmprendimientos implements Serializable {
.
.
.
@ManyToOne(optional = false)
private Emprendimientos emprendimiento;

The result of the findAll of Emprendimientos for the collection is

.
.
"logosEmprendimientos": [],

I have tow records in tha data base in the LogosEmprendimientos related to the Emprendimientos record in colsulting enter image description here

Why it retur empty and how can i solve it?

Thanks!

the father class (Emprendimientos) enter image description here

the son class (Cofundadores) enter image description here

that birng a infinite loop when I consult it

Upvotes: 1

Views: 1340

Answers (1)

gkatiforis
gkatiforis

Reputation: 1652

In @OneToMany relationship, the mappedBy is the field's name on the other class. In this case is emprendimiento:

public class Emprendimientos implements Serializable {
.
.
.
@OneToMany(cascade = CascadeType.ALL, mappedBy = "emprendimiento")
private Collection<LogosEmprendimientos> logosEmprendimientos;

For @ManyToOne relationship you have to add also @JoinColumn(name="EMPRENDIMIENTOS_ID") where the EMPRENDIMIENTOS_ID is the primary key in Emprendimientos table which is the foreign key in LogosEmprendimientos table.

public class LogosEmprendimientos implements Serializable {
.
.
.
@ManyToOne(optional = false)
@JoinColumn(name="EMPRENDIMIENTOS_ID")
private Emprendimientos emprendimiento;

Upvotes: 1

Related Questions