Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

hibernate one to many reationship.

 
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.
 
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



-Cameron McKenzie

 
Jigar Naik
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cameron

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.

Do i have any alternative ?
 
Cameron Wallace McKenzie
author and cow tipper
Posts: 5000
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm wondering if you might be able to configure the 'cascade-type' in the XML to address this. I'll dig...

-Cameron McKenzie
 
Jigar Naik
Ranch Hand
Posts: 763
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



output

 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic