【发布时间】:2015-06-30 02:21:43
【问题描述】:
我正在将 JPA 与 Hibernate 4.x. 和 postgresql 9.x 一起使用,但我想知道一些问题。
当使用@ElementCollection注解,并且我配置了Set类型嵌入字段时,我希望生成可嵌入集合表的主键,但是它不起作用。我只能找到一个针对关系所有者的外键。我想知道为什么它不生成主键。这是我的测试代码。 A.java
@Entity
public class A {
@Id
private Long id;
@Column(name = "\"as\"")
private String as;
public String getAs() {
return as;
}
public void setAs(String as) {
this.as = as;
}
@ElementCollection
private Set<B> bs = new HashSet<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Set<B> getBs() {
return bs;
}
public void setBs(Set<B> bs) {
this.bs = bs;
}
}
B.java
@Embeddable
public class B {
private String col1;
private String col2;
public String getCol1() {
return col1;
}
public void setCol1(String col1) {
this.col1 = col1;
}
public String getCol2() {
return col2;
}
public void setCol2(String col2) {
this.col2 = col2;
}
}
结果是
Hibernate:
create table A (
id int8 not null,
"as" varchar(255),
primary key (id)
)
Hibernate:
create table A_bs (
A_id int8 not null,
col1 varchar(255),
col2 varchar(255)
)
Hibernate:
alter table A_bs
add constraint FK_rcecll1ao3brmwwnsep3iqq3p
foreign key (A_id)
references A
告诉我为什么它没有生成主键? 先谢谢了~
【问题讨论】: