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.
|