java - JPA Join Column -
i have 3 tables in database.
first one(categories) includes: id, category, isactive
second one(menu) includes: parentid , childid.
third one(items) includes: id, name , price.
first table has relationship second. want relationship between second , third now.
second table has these values in database:
- 7(id), book1(name), 120(price)
- 7(id), book2(name), 100(price)
- 8(id), car1(name), 1620(price)
- 8(id), car2(name), 520(price)
- 8(id), car3(name), 5520(price)
- 9(id), house1(name), 10000(price)
- 9(id), house2(name), 11000(price)
- 9(id), house3(name), 15000(price)
at moment have joined column childid column id.
menu.java
@embeddedid menuid id; @column(name="parentid") private integer parentid; @column(name="childid") private integer childid; @onetoone @joincolumn(name="childid",referencedcolumnname="id") private categories subcategories; public categories getsubcategories() { return this.subcategories; } public void setsubategories(categories subcategories) { this.subcategories = subcategories; } @onetoone @joincolumn(name="parentid", referencedcolumnname="id") private categories categories; public categories getcategories() { return this.categories; } public void setcategories(categories categories) { this.categories = categories; } @onetoone @joincolumn(name="childid",referencedcolumnname="id") private items items; public items getitems() { return this.items; } public void setitems(items items) { this.items = items; }
items.java has no modifications subcategories.java
i using json object print out values:
jsonobject.put("parentid", menu.getparentid()); jsonobject.put("childid", menu.getchildid()); jsonobject.put("categories", menu.getcategories().getcategory()); jsonobject.put("subcategories", menu.getsubcategories().getcategory()); jsonobject.put("itemid", menu.getitems().getid()); jsonobject.put("product", menu.getitems().getproduct()); jsonobject.put("brand", menu.getitems().getbrand()); jsonobject.put("sum", menu.getitems().getsum());
it works, works not imagine on project. prints same values:
- 7(id), book1(name), 120(price)
- 7(id), book1(name), 120(price)
- 8(id), car1(name), 1620(price)
- 8(id), car1(name), 1620(price)
- 8(id), car1(name), 1620(price)
- 9(id), house1(name), 10000(price)
- 9(id), house1(name), 10000(price)
- 9(id), house1(name), 10000(price)
and on... not this:
- 7(id), book1(name), 120(price)
- 7(id), book2(name), 100(price)
- 8(id), car1(name), 1620(price)
- 8(id), car2(name), 520(price)
- 8(id), car3(name), 5520(price)
- 9(id), house1(name), 10000(price)
- 9(id), house2(name), 11000(price)
- 9(id), house3(name), 15000(price)
so question should use @onetomany method? or in wrong way?
using @onetoone
extending table (more columns) rows.
it seems trying set relation model orders/bills , positions of orders/bills.
normally (at least so) create "parent"-table orders.
table order: --------------- oder_id | customer | date | ... --------+----------+------+----- 1 | cust_1 | ... | ... 2 | cust_3 | ... | ...
the positions of order in "child"-table order_positions. foreign key constraint (order_position.order_id order.order_id) ensures data integrity.
table order_position: ----------------------- id | oder_id | item | price | ... ----+---------+----------+-------+----- 1 | 1 | book_1 | 100 | ... 2 | 1 | book_2 | 200 | ... 3 | 2 | car_1 | 2500 | ... 4 | 2 | car_2 | 234 | ...
to anser question: yes, in jpa should use @onetomany
in oder-entity , (if want bidirectional relationship) manytoone
in orderpostion-entity. take @ examples in objectdb-wiki
Comments
Post a Comment