-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Lesson learnt with Parent / Child relationship
PostPosted: Wed Sep 01, 2004 9:00 pm 
Newbie

Joined: Wed Sep 01, 2004 7:04 pm
Posts: 3
Location: Melbourne, Australia
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:


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 7:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
turn on sql logs

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 8:03 pm 
Newbie

Joined: Wed Sep 01, 2004 7:04 pm
Posts: 3
Location: Melbourne, Australia
Yeah, that was eventually how I solved the problem.

Another quick question though. Our project is a swing client that now has the requirement for database support. Do many people use Hibernate for this sort of project?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 02, 2004 8:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Yes, quite a few people do. However, I wouldn't say that this is what Hibernate was specifically designed for.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.