Reputation: 157
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
Why it retur empty and how can i solve it?
Thanks!
the father class (Emprendimientos)
that birng a infinite loop when I consult it
Upvotes: 1
Views: 1340
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