I have two database table Customer and Product which share one to many relationship. Now from the front end i am getting customer object and setting it into session. then i 2 product detials for the same customer and i set it into customer object.
So basically i will BE having List<Product> which will be of size 2.
Now when i insert customer object into database using hibernate. the primary key of the customer should get inserted into product table. which is not getting inserted.
So is there any michenism through which when my customer object gets inserted into database and database autogenerated customer's primary key gets inserted into Product table. ???
Or i have to explicitly manage ?
Bellow is my Customer.java
and bellow is my Product.java
and bellow is my main method.
In my Product table the customer_id is going null for all the product.
What happens when you save both sides of the relationship? Save the products and then save the Customer. You might have some more success. Here's a little example, perhaps even simlpler than your scenario, that successfully saves both sides of a one-to-many hibernate relationship:
Saving Both Sides of a One-To-Many Relationship with Hibernate and JPA
The pertinent code in the example is as follows. Notice that both sides of the relationship are being saved:
posted 11 years ago
Yes, that is right.
but i have four POJO (Customer, Product, License & Transaction) which have relationship with each other, each POJO contains 10 to 15 fields. and i have a single JSP page from which i am getting all the information in one request or multiple Ajax request(in case of one to many relationship). So basically i have integrated struts 2 and hibernate, so all of my form fields are automatically geting set in 4 pojo.
and in the case where i have one to many relationshio between i.e. in customer to products. i am making ajax call and saves all the product in HttpSession object. and finally on submit of JSP page i wanted to put all the POJOs to database.
And the way suggested by you, i have to do lot of getXXX and setXXX operations which is tedious as each pojo contains lots of fields.