Monday, 15 March 2010

sql - Mapping many-to-many relationship -



sql - Mapping many-to-many relationship -

i have many-to-many relationship :

customer, product, customer_product column quantity

i want map them hibernate question :

what loose if map them 2 one-to-many ? customer_product have 1 primary key (id_customer_product) , not composite primary key (id_product + id_customer)

customer entity:

@entity @table(name = "customer", catalog = "xx") public class client implements java.io.serializable { @onetomany(fetch = fetchtype.lazy, mappedby = "customer") private set customer_product = new hashset<customer_product>(0); }

product entity:

@entity @table(name = "product", catalog = "xx") public class client implements java.io.serializable { @onetomany(fetch = fetchtype.lazy, mappedby = "product") private set customer_product = new hashset<customer_product>(0); }

customer_product entity:

@entity @table(name = "customer_product", catalog = "xx") public class customer_product implements java.io.serializable { @id @generatedvalue(strategy = identity) @column(name = "id_customer_product") private integer id_customer_product; @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "id_customer") private client customer; @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "id_product") private product product; }

or have create answer , create composite primary key.

first, is client product? looks standard sales order model.

don't create simple many-to-many relationship. utilize one-to-many on both sides. you're on right track, though don't see quantity field anywhere.

a client hasmany customerproducts. product hasmany customerproducts. customerproduct belongsto client , belongsto product.

if want utilize auto-generated id field, primary key. can add together unique constraint on (customer,product) if don't want duplicate lines client , product.

sql database hibernate jpa

No comments:

Post a Comment