问题:绑定到Postgres表后,我的实体列表为空。

研究:JavaEE7,Java8,JPA2.1。该实体的OneToMany和其他实体中类似的ManyToMany绑定字段在同一数据库上也可以正常工作。

表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
@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:
}


我在REST服务中创建实体,例如:

IndicatorType indicatorType = comparisonDao.getIndicatorTypeById(1);


并得到java - JPA ManyToMany列表为空-LMLPHP

问题:关于使它起作用的任何想法。代码还需要什么?

最佳答案

级联丢失。下面是示例:



@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;

09-30 18:38