java - JPA with Hibernate 4.x, @ElementCollection, Primary Key -
i using jpa hibernate 4.x., , postgresql 9.x, wonder problem.
when using @elementcollection annotation, , configured set type embedded field, expect generate primary key of embeddable collection table, it's not working. can find 1 foreign key against owner of relationship. wonder why not generate primary key. test code. a.java
@entity public class { @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; } }
the result is
hibernate: create table ( 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
let me know why did not generate primary key? in advance~
because @elementcollection
used mapping collections of simple elements, not same entities. thing need reference owning entity generated hibernate (column a_id
, foreign key (a_id) references a
). take @ this post more information.
if need primary key in embeddable object, consider making proper entity.
Comments
Post a Comment