gpt4 book ai didi

java - JPA ManyToMany 列表为空

转载 作者:行者123 更新时间:2023-12-02 10:30:02 25 4
gpt4 key购买 nike

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

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

表 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);

并获取enter image description here

问题:任何关于使其发挥作用而错过的内容的想法。代码还需要什么?

最佳答案

缺少

级联。下面是示例:

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

关于java - JPA ManyToMany 列表为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53666253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com