为什么hibernate会产生不必要的约束?

eanckbw9  于 2021-07-04  发布在  Java
关注(0)|答案(1)|浏览(345)

在hibernate中,由双向关系生成的sql有一个奇怪的问题。这是密码。
首先 @OneToMany 侧面:

@Entity
@Table(name = "employers")
public class Employer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "eid")
    private Long employerId;

    private String name;

    @OneToMany(mappedBy = "titleId")
    private Set<Title> titles = new TreeSet<>(); 

    // Getters/setters...
}

以及 @ManyToOne 侧面:

@Entity
@Table(name = "titles")
public class Title {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "tid")
    private long titleId;

    /**
     * Not sure if I need a bi-directional relation here.
     */
    @ManyToOne
    @JoinColumn(name = "employerId")
    private Employer employer;

    // Getters/setters...
}

我对springboot和hibernate还比较陌生,所以这是我第一次尝试运行它。根据我参考的教程和文档,在我的例子中生成的sql是错误的。
下面是生成的sql( Postgres9Dialect )

CREATE TABLE public.items
(
    item_id bigint NOT NULL DEFAULT nextval('items_item_id_seq'::regclass),
    text character varying(255) COLLATE pg_catalog."default",
    title_tid bigint,
    CONSTRAINT items_pkey PRIMARY KEY (item_id),
    CONSTRAINT fkkwhqrl3vscoqcacws6kgsdlx5 FOREIGN KEY (item_id)
        REFERENCES public.titles (tid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT fkluhgxmnakeuroph186b2k05eq FOREIGN KEY (title_tid)
        REFERENCES public.titles (tid) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
)

问题在于这个约束:

CONSTRAINT fkkwhqrl3vscoqcacws6kgsdlx5 FOREIGN KEY (item_id)
REFERENCES public.titles (tid) MATCH SIMPLE
   ON UPDATE NO ACTION
   ON DELETE NO ACTION,

如果标题id( tid )超过雇主ID的最大数目(雇主表中的eid)。生成的雇主表是正确的。
如果手动删除此约束,则它将按预期工作。
为什么会产生这种错误的约束?我错过了什么?
(顺便说一句,书名指的是某个特定雇主的书名,而不是书名,所以我不打算把雇主和作者教程结合起来)

bwntbbo3

bwntbbo31#

应按以下方式更正Map:

@Entity
@Table(name = "employers")
public class Employer {
    @OneToMany(mappedBy = "employer")
    private Set<Title> titles = new TreeSet<>(); 
    // ...
}

@Entity
@Table(name = "titles")
public class Title {

    @ManyToOne
    @JoinColumn(name = "employerId")
    private Employer employer;
    // ...
}

这个 mappedBy 应包含关联另一方的字段名。

相关问题