在spring boot中,一个外键列可以链接到多个实体吗

dy1byipe  于 2023-04-11  发布在  Spring
关注(0)|答案(1)|浏览(108)

我试图将一个外键链接到多个3个实体,因此外键列可以将这3个实体中的一个主键作为值,这可能吗?
在这段代码中,我试图将这3个外键分配给列操作

public class workflow {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_operation", insertable=false, updatable=false)
    private demande_de_conge demande_de_conge;
    
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_operation", insertable=false, updatable=false)
    private annulation annulation;
    
    @JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_operation", insertable=false, updatable=false)
    private regularisation regularisation;
}
public class demande_de_conge {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id_demande;
    
    @JsonIgnore
    @OneToMany(mappedBy="demande_de_conge", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<workflow>workflow;

}
public class regularisation {
    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id_regularisation;
    
    @JsonIgnore
    @OneToMany(mappedBy="regularisation", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<workflow>workflow;

}
public class annulation {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id_annulation;
    
    @JsonIgnore
    @OneToMany(mappedBy="annulation", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<workflow>workflow; 
}
jm81lzqq

jm81lzqq1#

在你的情况下,你可以试试这个:

@JoinColumn(name = "demande_de_conge", referencedColumnName = "ID", foreignKey = @ForeignKey(name = "FK_NAME"))

相关问题