• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Hibernate 3.0 : Where am I going wrong?

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
shankhas sanyal
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 377
Android Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic