File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Unable to save data into child table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Unable to save data into child table" Watch "Unable to save data into child table" New topic

Unable to save data into child table

Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
Hi All,

I think I am doing something that is very basic, incorrectly.
I have a parent child relation ship in my db and I am trying to save data.Data gets properly saved into parent table but it does not get saved into child table.

I have defined OneToMany relation in parent table and ManyToOne in child table.All of import statements are importing javax.persistence classes (can that be an issue?).

Another thing is that child class is child of two classes(It has two columns that have ManyToOne relations).Is that an issue?

Parent class :-

Child class -

Data should be saved in CustomerCsoProfileMap when data is inserted in CsoProfile.There is no error in logs.Also I see the insert statement for parent class and also the sequencename.nextval for parent class sequence in logs.I do not see any statement in logs for child class at all.
What am I doing wrong?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
What code do you use to persist the objects? Do you get any errors?

Make sure you persist both objects.
Ensure you set both sides of the relationship (add to 1-m and set m-1 back).

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
Thanks James.So you mean I have to write code to save data into both Parent and child tables?In that case,what is the use of putting relationship between parent and child(one-to-many and many-to-one)?
Just for getting all children from parent and getting parent from child?
Jitesh Sinha
Ranch Hand

Joined: Jun 19, 2004
Posts: 146
Can someone please reply to above post?

I have another question as well(somewhat related).
Suppose I have a parent entity and child entity.I have set up one-to-many in parent and many-to-one in child correctly.
Now when setting up parent's attribute,I set the attribute of embedded child object as well.Something like this -

child.setOneAttribute("something") ;
child.setAnother("something") ;

parent.setAttributeA("something") ;
parent.setAttributeB("something") ;
parent.setChildJoinColumn(child) ; ;//or maybe getHibernateTemplate().save(parent) ;

will it save the data in child entity as well?
Let me know if you want me to clarify anything.

I agree. Here's the link:
subject: Unable to save data into child table
It's not a secret anymore!