Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes Hibernate 3.0 : Where am I going wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate 3.0 : Where am I going wrong?" Watch "Hibernate 3.0 : Where am I going wrong?" New topic
Author

Hibernate 3.0 : Where am I going wrong?

shankhas sanyal
Ranch Hand

Joined: Jun 15, 2008
Posts: 45
Hi all!
I am very new to Hibernate. I am trying to save 2 records with one save. Im using mysql as my backend database.
i have been at this problem for the better part of 2 weeks without any solution.

So please help me.

my database table structure:
child table

Parent table


parent_childjointable


the following are my files in the project.
1. Parent.hbm.xml


2. child.hbm.xml


3. parentchildjtable.hbm.xml



4.Parent.java




5. Child.java





6. parentchildjtable.java




this is the code inside my main method:





these are the errors i am getting :





Please help me where am i going wrong


I love this place!
Keshav Prasad gurukul
Greenhorn

Joined: Feb 25, 2009
Posts: 14


change the generator class to incerment,hilo or other auto-numeric hibernate class.


SCJP5 94% SCWCD5 86% SCBCD5 86%
shankhas sanyal
Ranch Hand

Joined: Jun 15, 2008
Posts: 45
Keshav Prasad gurukul wrote:

change the generator class to incerment,hilo or other auto-numeric hibernate class.




thanks Keshav!
now after implementing this (i used increment)... neither the parent table nor the child table are getting inserted with the data
now i am getting these error messages:



and please have a look at the sql statements on the first 3 lines :

Hibernate: insert into parent (father_or_mother, parent_firstname, parent_lastname, parent_id) values (?, ?, ?, ?)
Hibernate: update child set parent_id=? where child_id=?
Hibernate: update child set parent_id=? where child_id=?

why is it trying to update when my real intention is to insert new records into the child table
and how do i fix this?
Keshav Prasad gurukul
Greenhorn

Joined: Feb 25, 2009
Posts: 14
Looking at your code, i see that you are explicitly managing relationship between the parent and child. You can implement relationship mapping using Hibernate only. That way it would be a better architecture. But currently i am reviewing your code
shankhas sanyal
Ranch Hand

Joined: Jun 15, 2008
Posts: 45
Keshav Prasad gurukul wrote:Looking at your code, i see that you are explicitly managing relationship between the parent and child. You can implement relationship mapping using Hibernate only. That way it would be a better architecture. But currently i am reviewing your code


Hi Keshav
Ok so what do you suggest I do?
thank you for reviewing my code!
Sean Clark
Rancher

Joined: Jul 15, 2009
Posts: 377

Hey,

In my opinion, the schema that you have designed is slightly flawed and that is maybe what is causing the issues.
You have a parent_id column, which maps into join table, so when you try to insert a child, it tries to insert into the join table which doesn't have a default for child. I think this might be why the error occurs.

In your Child hbxml I also think that there should be a set of parents.
You may also want to use the many to many tags so that you don't need to explicitly create the jointable XML file/java classes

With regards to your schema, you seem to have a many-to-many join between parent and child, so one parent can have many children and one child can have many parents. If you are refering to real life here, it is more likely that a child has 0, 1 or 2 parents only. If you are not then just ignore me!

Sean


I love this place!
shankhas sanyal
Ranch Hand

Joined: Jun 15, 2008
Posts: 45
Sean Clark wrote:Hey,

In my opinion, the schema that you have designed is slightly flawed and that is maybe what is causing the issues.
You have a parent_id column, which maps into join table, so when you try to insert a child, it tries to insert into the join table which doesn't have a default for child. I think this might be why the error occurs.



Yea Sean that's exactly what is happening.. So what do you suggest? I remove the foreign keys? which ones?

Sean Clark wrote:
In your Child hbxml I also think that there should be a set of parents.
You may also want to use the many to many tags so that you don't need to explicitly create the jointable XML file/java classes

Do i need the join tag? could you please show me how to define the Set and many to many tag? or any sites or references ?


Sean Clark wrote:
With regards to your schema, you seem to have a many-to-many join between parent and child, so one parent can have many children and one child can have many parents. If you are refering to real life here, it is more likely that a child has 0, 1 or 2 parents only. If you are not then just ignore me!



Do you have any ideas how to make the schema flawless?


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hibernate 3.0 : Where am I going wrong?
 
Similar Threads
one to many mapping(unidirection)
Problem in mapping
Hibernate mapping : Parent ->Child(Male or Female)
Problem in <one-to-one> relation mapping
Cannot add or update a child row: a foreign key constraint fails