Reputation: 19918
PROBLEM: My entity list is empty after binding to Postgres table.
RESEARCH: JavaEE7, Java8, JPA2.1. OneToMany for this entity and similar ManyToMany binded fields in other entities work fine with the same database.
Table DDL:
create table clean.report_type_to_indicator_type(
indicator_type_id integer not null
constraint report_type_to_indicator_type_indicator_type_id_fk
references indicator_type,
report_type_id integer not null
constraint report_type_to_indicator_type_report_type_id_fk
references report_type,
constraint report_type_to_indicator_type_pk
primary key (indicator_type_id, report_type_id)
);
create unique index
report_type_to_indicator_type_indicator_type_id_report_type_id_
on clean.report_type_to_indicator_type (indicator_type_id,
report_type_id);
Entity:
@Entity
@Table(schema = "clean")
@JsonIgnoreProperties(ignoreUnknown = true)
public class IndicatorType {
// Fields:
@Id @GeneratedValue private int id;
private String name;
@ManyToOne private Unit unit;
@ManyToOne private PeriodType periodType;
@ManyToOne private RegionType regionType;
@ManyToMany
@JoinTable(
schema = "clean",
name = "report_type_to_indicator_type",
joinColumns = @JoinColumn(name = "indicator_type_id"),
inverseJoinColumns = @JoinColumn(name = "report_type_id"))
private List<RegionType> reportTypes;
// Getters and setters:
}
I create entity in REST-service like:
IndicatorType indicatorType = comparisonDao.getIndicatorTypeById(1);
QUESTION: Any ideas of what is missed to make it work. What else is required from code?
Upvotes: 0
Views: 1920
Reputation: 19918
As expected, it was a simple typo-error 34+ viewers have also missed. I've tried to add parameter to annotation:
@ManyToMany(mappedBy = "")
and asked Intellij for suggestions in double quotes. Suprisingly it has suggested fields from another entity - RegionType. The error was in the list type:
private List<RegionType> reportTypes;
-> private List<ReportType> reportTypes;
Upvotes: 0
Reputation: 61
Cascading is missing. Below is the example :
@ManyToMany(
cascade = {CascadeType.ALL},
fetch = FetchType.LAZY
)
@JoinTable(
name = "report_type_to_indicator_type",
joinColumns = {@JoinColumn(name = "indicator_type_id")},
inverseJoinColumns = {@JoinColumn(name = "report_type_id")}
)
private List<RegionType> reportTypes;
Upvotes: 1