File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate 3.0 : Where am I going wrong?

 
shankhas sanyal
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Keshav Prasad gurukul
Greenhorn
Posts: 14
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
shankhas sanyal
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 377
Android Java Spring
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shankhas sanyal
Ranch Hand
Posts: 45
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic