hibernate - onetomany unidirectional with jointable setup using jpa -


i have 2 entities namely customer , order in onetomany relationship. 1 customer can have multiple orders. since needed relationship unidirectional, using jointable.

i able add entries customer entity using jpa. able add entries order entity using jpa.

i wondering how connect 2 data. let's have 1 entry in customer table , 2 entries in order table. associate these 2 entries in order table 1 entry in customer table.

currently, don't see entries in jointable customer_order. how make association? guess while adding orders order table, have mention customer id number somehow. not sure how that. there criteria query in jpa?

thanks.

customer class -

@entity public class customer implements serializable {     @id     @generatedvalue(strategy = generationtype.table, generator = "generatorcustomer")     @tablegenerator(name = "generatorcustomer", allocationsize = 1)     @column(name="customer_id")     private long id;     public long getid() {         return id;     }      public void setid(long id) {         this.id = id;     }      @onetomany()     @jointable (         name="customer_order",         joincolumns={ @joincolumn(name="customer_id", referencedcolumnname="customer_id") },         inversejoincolumns={ @joincolumn(name="order_id", referencedcolumnname="order_id", unique=true) }     )     private list<order> orderlist = new arraylist<order>();     public list<order> getorderlist() {         if(this.orderlist == null) {             this.orderlist = new arraylist<order>();         }         return this.orderlist;     }      public void setorderlist(list<order> orderlist) {         this.orderlist = orderlist;     }      public void addorder(order order) {         this.orderlist.add(order);     }      /* other logic follows......... */ } 

order class -

@entity public class order implements serializable { @id @generatedvalue @column(name="order_id") private long id; public long getid() {     return id; }  public void setid(long id) {     this.id = id; }  /* other logic follows......... */ } 

customer table description:

jbossql=# \d customer column   |         type          |                        modifiers -------------+-----------------------+--------------------------------------------------------------- customer_id | bigint                | not null default nextval('customer_customer_id_seq'::regclass) name        | character varying(50) |  indexes: "customer_pkey" primary key, btree (customer_id) referenced by: table "customer_order" constraint "fk8ef2f420ec016855" foreign key (customer_id) references customer(customer_id) 

order table description:

jbossql=# \d order column   |  type  | modifiers  ------------+--------+----------- order_id   | bigint | not null value      | real   |  indexes: "order_pkey" primary key, btree (order_id) referenced by: table "customer_order" constraint "fk1e4828a98187660" foreign key (order_id) references order(order_id) 

customer_order joint table description:

jbossql=# \d customer_order column       |  type  | modifiers  --------------+--------+----------- customer_id  | bigint | not null order_id     | bigint | not null indexes: "customer_order_order_id_key" unique constraint, btree (order_id) foreign-key constraints: "fk1e4828a98187660" foreign key (order_id) references order(order_id) "fk1e4828adba386b8" foreign key (customer_id) references customer(customer_id) 

i able insert item customer table:

jbossql=# select * customer; customer_id | name  -------------+------------- 1        | joe (1 row) 

i able insert items oder table well:

jbossql=# select * order; order_id | value  ----------+----------- 1       |  1.8 2       |  0.5 (2 rows) 

i thinking customer_order table automatically populated i.e hibernate take of that. appears not because jointable empty:

jbossql=# select * customer_order; customer_id | order_id  -------------+----------- (0 rows) 

so, intention make 2 order entries connected customer joe.

please help.

  1. do have explicitly add record customer_order table?
  2. if not, how insert items order table can connect entry particular customer?
  3. are mappings right in java files onetomany [unidirection] relationship work expected
  4. i using jpa2 , jboss , hibernate. have code references test one-to-many relationship? references complete project or reading material help.

thanks looking.

it's extremely simple:

customer.addorder(order); 

that's need. that's principle of orm. manipulate objects, , orm saves them in database using mapping defined.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -