-->
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.  [ 2 posts ] 
Author Message
 Post subject: Unable to persist object with one saveOrUpdate() call
PostPosted: Sun Feb 06, 2005 10:03 pm 
Newbie

Joined: Sun Feb 06, 2005 9:38 pm
Posts: 1
Hibernate version:
2.1
Mapping documents:
Parent.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="org.hibernate.auction.Parent">
<id name="id" column="parent_id" unsaved-value="null">
<generator class="native"/>
</id>
<set name="children" inverse="true" cascade="save-update">
<key column="parent_id"/>
<one-to-many class="org.hibernate.auction.Child"/>
</set>
</class>

</hibernate-mapping>

*****************************
Child.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="org.hibernate.auction.Child">
<id name="id" column="child_id" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name"/>

<many-to-one name="parent" class="org.hibernate.auction.Parent" column="parent_id" not-null="true" cascade="save-update"/>
</class>

</hibernate-mapping>
*******************************
Code between sessionFactory.openSession() and session.close():
Parent p = new Parent();
Child c = new Child();
c.setName("Olli");
c.setParent(p);
p.getChildren().add(c);
s.saveOrUpdate(p);
s.flush();

Code of Parent.java and Child.java:
PARENT.java:

package org.hibernate.auction;
import java.util.*;

public class Parent {
private long id;
private Set children;

Parent(){
children = new HashSet();
}

public long getId() { return id; }
private void setId(long id) { this.id=id; }

public Set getChildren() { return children; }
public void setChildren(Set children) { this.children=children; }

}
*********************************
Child.java:
package org.hibernate.auction;

public class Child {
private long id;
private String name;
private Parent parent;

public long getId() { return id; }
private void setId(long id) { this.id=id; }

public String getName() { return name; }
public void setName(String name) { this.name=name; }

public Parent getParent(){
return parent;
}
public void setParent(Parent parent){
this.parent = parent;
}
}

Full stac k trace of any exception that occurs:
Could not synchronize database state with session
[java] net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
[java] at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
[java] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:663)
[java] at net.sf.hibernate.persister.EntityPersister.update(EntityPersister.java:623)
[java] at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:52)
[java] at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2438)
[java] at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2392)
[java] at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2260)
[java] at org.hibernate.auction.Main.create(Main.java:44)
[java] at org.hibernate.auction.Main.main(Main.java:91)

Name and version of the database you are using:
MYSQL Ver 12.22 Distrib 4.0.23, for pc-linux-gnu (i386)

As you can see, my program consists of two classes, Parent and Child. Parent has a Set of type Child. Thus, there simply is a one-to-many relation between Parent and Child

After executing my program, the tables remain empty. Both tables, Parent and Child, were automatically generated by invoking the command
Configuration cfg = new Configuration()
.addClass(Parent.class)
.addClass(Child.class)
.setProperty(Environment.HBM2DDL_AUTO, "create");

If I change the source code to:
Parent p = new Parent();
s.save(p);
Child c = new Child();
c.setName("Olli");
c.setParent(p);
s.save(c);
p.getChildren().add(c);
s.saveOrUpdate(p);
s.flush();

everything works fine, i.e. there are correct entries in the DB-tables and no exceptions, but this solution is rather useless for me! It should work with just one save-command, right? Furthermore, if I now load a parent from the database, there are no Child objects in the Set 'children' of parent!

I hope, somebody in here can help me on this! Any help would be really appreciated!

-- Oliver.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 07, 2005 3:37 am 
Newbie

Joined: Mon Aug 09, 2004 7:29 am
Posts: 10
Location: India
Change the type of id to Long (instead of long).
Or change the unsaved-value attribute to -1 and in the constructor initialize id to -1.

Hibernate uses the unsaved-value attribute to figure out if its a create or update. If the id value matches the unsaved-value then it is an create, otherwise it is an update.

In your case since the unsaved-value is specified incorrectly, so a create is being interpreted as an update.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.