Hibernate JPA无法从数据库中识别现有约束
在我的应用程序启动期间,hibernate通过添加新的约束来更改我的表。下面是示例代码,可以全面了解我的问题 Postgresql模式Hibernate JPA无法从数据库中识别现有约束,hibernate,jpa,orm,postgresql-9.1,Hibernate,Jpa,Orm,Postgresql 9.1,在我的应用程序启动期间,hibernate通过添加新的约束来更改我的表。下面是示例代码,可以全面了解我的问题 Postgresql模式 create table public."A" { a_id serial primary key } create table public."B" { b_id serial primary key } create table public."C"( a_id integer not null references public
create table public."A" {
a_id serial primary key
}
create table public."B" {
b_id serial primary key
}
create table public."C"(
a_id integer not null references public."A" (a_id),
b_id integer not null references public."B" (b_id),
)
数据库模型
class A{
@Id
@Column(name = "a_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Integer aId;
}
class B {
@Id
@Column(name = "b_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE)
public Integer bId;
}
class C {
@ManyToOne
@JoinColumn(name = "a_id")
public A a;
@ManyToOne
@JoinColumn(name = "b_id")
public B b;
}
在我的应用程序启动时,它再次创建约束
Hibernate: alter table public."C" add constraint FKk4caeiw8tynv7g0p13h5inaht foreign key (a_id) references public."A"
Hibernate: alter table public."C" add constraint FK4bsokehyo37wygp12onlu8fbl foreign key (b_id) references public."B"
在DB中,我看到约束已经可用
CONSTRAINT "C_a_id_fkey" FOREIGN KEY (a_id)
REFERENCES public."A" (a_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT "C_b_id_fkey" FOREIGN KEY (b_id)
REFERENCES public."B" (b_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
除此之外,我还看到了两个新的约束
CONSTRAINT FKk4caeiw8tynv7g0p13h5inaht foreign key (a_id)
REFERENCES public."A" (a_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT FK4bsokehyo37wygp12onlu8fbl foreign key (b_id)
REFERENCES public."B" (b_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
这里可能有什么问题?这是因为Hibernate正在比较约束名称,并注意到指定名称不存在约束,因此它会重新创建它,认为它不存在 一种解决方法是在注释中按名称手动指定约束:
@JoinColumn(name = "...", foreignKey = @ForeignKey(name = "..."))
注意:Hibernate 5.2.7之前的版本存在一些问题,其中某些注释没有坚持将外键注释的值正确地传递给数据库模式工具。我相信@JoinColumn
很好,但如果不是这样的话,使用5.2.8有我介绍的所有修复,以避免忽略这些注释
另一个解决办法显然是删除现有的约束,只允许Hibernate使用自己的命名策略将它们添加回来。您在
persistence.xml
中定义了哪种模式生成策略(Hibernate.hbm2ddl.auto/avax.persistence.schema generation.database.action
)?