【发布时间】:2017-07-17 05:43:15
【问题描述】:
在我的应用启动期间,休眠通过添加新的约束来改变我的表。这是完整了解我的问题的示例代码。
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."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,
除了这些,我还看到了 2 个新的约束
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
这里有什么问题?
【问题讨论】:
-
你在
persistence.xml中定义了哪个模式生成策略(hibernate.hbm2ddl.auto/avax.persistence.schema-generation.database.action)?
标签: hibernate jpa orm postgresql-9.1