This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes hibernate one to many reationship. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate one to many reationship." Watch "hibernate one to many reationship." New topic
Author

hibernate one to many reationship.

Jigar Naik
Ranch Hand

Joined: Dec 12, 2006
Posts: 755
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.


Jigar Naik


Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Dec 12, 2006
Posts: 755
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
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Dec 12, 2006
Posts: 755



output

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate one to many reationship.