如何在春季启动中修复我的双向一对多关系



我正在尝试在两个表之间建立一对多关系,其中一个是聚合根表(容器(。但是当我创建一个新实体时,多对一表不会将其外键映射到一对多表的主键。我做错了什么?

容器表(一对多(

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "vessel_info")
@SequenceGenerator(name = "vessel_id_seq",sequenceName = "vessel_id_seq", initialValue = 1,allocationSize = 1)
public class Vessel {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="vessel_id_seq" )
    @Column(name = "vessel_code")
    private Long vesselCode;
    private String name;
    private Long companyId;
    private Long  imo;
    private String type;
    @Column(name = "fleet_id")
    private Long fleetId;
    private String yard;
    private Integer hn;
    private Date delivered;
    private Double age;

    @OneToMany(mappedBy = "vessel",cascade = CascadeType.ALL,
            targetEntity = BoilerInfo.class, orphanRemoval = true,fetch = FetchType.EAGER)
    private List<BoilerInfo> boilerInfo ;

锅炉信息表(多对一(

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "boiler_info")
@SequenceGenerator(name = "boiler_id_seq",sequenceName = "boiler_id_seq", initialValue = 1,allocationSize = 1)
public class BoilerInfo {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="boiler_id_seq" )
    private Long id;

    private String maker;
    private String type;
    private String tubeType;
    private String exhGasByPass;
    @ManyToOne(fetch = FetchType.EAGER, optional = false,targetEntity = Vessel.class)
    @JoinColumn(name="vessel_code",nullable = false)
    private Vessel vessel;

}

创建容器

private final VesselRepository vesselRepository ;
@Override
public Vessel create(Vessel entity) {
    log.info("Creating {}.", entity);
    Vessel vessel = vesselRepository.save(entity);
    return vessel;
    }

杰伦斯发布

{   
    
    "fleetId":"1",
    "name":"BOILERTEST",
    "type":"temp",
    "companyId":"1",
    "boilerInfo":[{
        "maker": "temp",
        "type":"temp"
    }]
}

来自 SQL 查询的日志

设置关系的两边:

vesel.getBoilerinfos().add(boilerInfo);
boilerInfo.setVesel(vesel);

相关内容

  • 没有找到相关文章

最新更新