java - Understanding mappedBy annotation in Hibernate -
i trying understand mappedby attribute of @onetomany annotation in jpa. created below example customer has list of orders:
@entity public class customer { @id @generatedvalue public integer getid() { return id; } public void setid(integer id) { this.id = id; } private integer id; @onetomany(mappedby="customer") @ordercolumn(name="orders_index") public list<order> getorders() { return orders; } public void setorders(list<order> orders) { this.orders = orders; } private list<order> orders; } @entity @table(name="tbl_order") public class order { @id @generatedvalue public integer getid() { return id; } public void setid(integer id) { this.id = id; } private integer id; public int getordernumber() { return ordernumber; } public void setordernumber(int ordernumber) { this.ordernumber = ordernumber; } private int ordernumber; @manytoone public customer getcustomer() { return customer; } public void setcustomer(customer customer) { this.customer = customer; } private customer customer; } now when use hibernate generate tables see hibernate created 2 tables:
hibernate: create table customer (id number(10,0) not null, primary key (id)) hibernate: create table tbl_order (id number(10,0) not null, ordernumber number(10,0) not null, customer_id number(10,0), orders_index number(10,0), primary key (id)) hibernate: alter table tbl_order add constraint fk_nt24krtgqwcsynosqgk4jkvfv foreign key (customer_id) references customer also if try save customer , orders see below dml statements generated hibernate:
hibernate: insert customer (id) values (?) hibernate: insert tbl_order (customer_id, ordernumber, id) values (?, ?, ?) hibernate: update tbl_order set orders_index=? id=? why hibernate tried insert , updates record in tbl_order instead of running single insert query?
now if remove mappedby attribute , try generate tables, see 3 tables time:
hibernate: create table customer (id number(10,0) not null, primary key (id)) hibernate: create table customer_tbl_order (customer_id number(10,0) not null, orders_id number(10,0) not null, orders_index number(10,0) not null, primary key (customer_id, orders_index)) hibernate: create table tbl_order (id number(10,0) not null, ordernumber number(10,0) not null, customer_id number(10,0), primary key (id)) hibernate: alter table customer_tbl_order add constraint uk_sw94jktvh72tripj876s31052 unique (orders_id) hibernate: alter table customer_tbl_order add constraint fk_sw94jktvh72tripj876s31052 foreign key (orders_id) references tbl_order hibernate: alter table customer_tbl_order add constraint fk_f03up2h945cg0dcbo2pdb1d3c foreign key (customer_id) references customer hibernate: alter table tbl_order add constraint fk_nt24krtgqwcsynosqgk4jkvfv foreign key (customer_id) references customer why hibernate creates additional table in case? how mappedby attribute controlling this? why have additional unique key constraint on orders_id column in customer_tbl_order table?
now if try save customer , orders below dml operations:
hibernate: insert customer (id) values (?) hibernate: insert tbl_order (customer_id, ordernumber, id) values (?, ?, ?) hibernate: insert customer_tbl_order (customer_id, orders_index, orders_id) values (?, ?, ?) why don't have additional update query in case compared case have declared mappedby attribute?
that's normal.
with mappedby, directly tell hibernate/jpa 1 table owns relationship, , therefore stored column of table.
without, relationship external , hibernate/jpa need create table store relationship.
example:
- a stackoverflow
questionhave severalanswer. - an
answerowned 1 , 1question.
in plain jdbc, create 2 table:
questions(question_id, ...); answers(answer_id, question_id, ...); where question_id foreign key referencing question.question_id.
as other case, don't have real case since there every time many many a unique constraint (eg: question may have several answer, , answer may have physically have several question, appears once question).
Comments
Post a Comment