Hi all,
I have just started using Hibernate and after reading some of this forum, I found myself in the boat of having trouble getting my first parent /child relationship working.
The scenario is two tables with natively generated keys with a one - to - many relationship between parent and child.
After reading chapter 16 of the manual I tried to get the Cascading Lifecycle implementation working. After a couple of hours of "Could not synchronize database state with session". I found that my problem was using an int instead of an Integer as my primary key field. This wasn't obvious from reading the manual
I know this is probably obvious to anyone that has been using Hibernate for a while, but it was a gotcha that had me stumped for a while.
btw. I am really impressed with Hibernate. Documentation: awesome! Forum: ace! Wiki, getting there but, still good!
Luke Matthews.
Hibernate version:
2.1.6
Mapping documents:
Code:
<hibernate-mapping>
<class name="Parent" table="parent">
<id name="id" type="integer" unsaved-value="null">
<column name="id" sql-type="int(11)" not-null="true"/>
<generator class="native"/>
</id>
<bag name="tracks" inverse="true" cascade="all">
<key column="id"/>
<one-to-many class="CycloneTrack"/>
</bag>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Child" table="child">
<id name="id" type="integer" unsaved-value="null">
<column name="id" not-null="true"/>
<generator class="native"/>
</id>
<many-to-one name="parent" class="Parent"
column="parent" not-null="true"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Parent parent = (Parent)session.load(Parent.class, new Integer(1));
Child child = new Child();
parent.addChild(child);
session.flush();
where Parent code is
Code:
public class Parent
{
private Integer id;
//getters and setters for id and constructor removed for brevity
public void addChild(Child child)
{
child.setParent(this);
children.add(child);
}
and Child code is
Code:
public class Child
{
private Integer id;
//getters and setters for id and constructor removed for brevity
public void setParent(Child child)
{
child.setParent(this);
children.add(child);
}
Full stack trace of any exception that occurs:
[java] 1/09/2004 16:37:35 net.sf.hibernate.impl.SessionImpl execute
[java] SEVERE: Could not synchronize database state with session
[java] net.sf.hibernate.HibernateException: Batch update row count wrong: 0
Name and version of the database you are using:
MySQL
Debug level Hibernate log excerpt: