[英]Why hibernate generates unnecessary constraint?
I'm having a strange problem with the SQL generated by a bidirectional relationship in Hibernate.我在 Hibernate 中由双向关系生成的 SQL 有一个奇怪的问题。 Here's the code.
这是代码。
First the @OneToMany
side:首先是
@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...
}
And the @ManyToOne
side:而
@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...
}
I'm fairly new to springboot and hibernate, so this is the first thing I've attempted that would actually run.我对 springboot 和 hibernate 相当陌生,所以这是我尝试的第一件事,它实际上可以运行。 Based on the tutorials and the documentation I've referenced, the SQL generated in my case is wrong.
根据我参考的教程和文档,在我的案例中生成的 SQL 是错误的。
Here's the generated SQL ( Postgres9Dialect
)这是生成的 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
)
The problem is this constraint:问题是这个约束:
CONSTRAINT fkkwhqrl3vscoqcacws6kgsdlx5 FOREIGN KEY (item_id)
REFERENCES public.titles (tid) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION,
It has the effect of not allowing additional titles from being created if the title id ( tid
) exceeds the highest number of employer ids (eid in the employer table).如果头衔 id (
tid
) 超过雇主 id 的最大数量(雇主表中的 eid),它会导致不允许创建其他头衔。 The generated employers table is correct.生成的雇主表是正确的。
If I delete this constraint manually, then it works as expected.如果我手动删除此约束,则它会按预期工作。
Why is it creating this erroneous constraint?为什么要创建这个错误的约束? What am I missing?
我错过了什么?
(as an aside, title refers to a title one would have for a particular employer, not a book title, so I am not trying to combine employer & author tutorials) (顺便说一句,标题是指特定雇主的标题,而不是书名,所以我不想将雇主和作者教程结合起来)
You should correct your mapping in this way:您应该以这种方式更正您的映射:
@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;
// ...
}
The mappedBy
should hold the field name of another side of association. mappedBy
应该保存关联另一侧的字段名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.