Hi,
I'm trying out the composite pattern example from the Hibernate site (
http://hibernate.org/86.html).
Following errors occured:
1/
Configuring using the defaultCache settings.
java.lang.ClassCastException
at net.sf.hibernate.type.SetType.wrap(SetType.java:24)
2/
ERROR SessionImpl:2368 - Could not synchronize database state with session
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
at net.sf.hibernate.impl.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:25)
...
net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)
...
Here is the code I used to test the composite pattern:
Code:
public class TestKlasse {
private SessionFactory sessions;
public void configure() throws HibernateException {
sessions = new Configuration()
.addClass(Site.class)
.setProperty(Environment.HBM2DDL_AUTO, "create")
.buildSessionFactory();
}
public static void main(String[] args) throws HibernateException {
TestKlasse T = new TestKlasse();
Collection l = new ArrayList();
Site site = new Site(null,null,"test1","test2","test3");
Site child1 = new Site(null, site,"test1","test1","test1");
Site child2 = new Site(null, site,"test2","test2","test2");
l.add(child1);
l.add(child2);
site.setChildrenSites((Collection)l);
T.configure(); //
Session session = T.sessions.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(site);
tx.commit();
}
catch (HibernateException he) {
if (tx!=null) tx.rollback();
throw he;
}
finally {
session.close();
}
}
}
Persistent class:
Code:
public class Site {
private Integer id;
private long version;
private Collection childrenSites;
private Site parent;
private String name;
private String description;
private String namespace;
public Site(){
}
public Site(Collection childrenSites, Site parent, String name, String description, String namespace) {
this.childrenSites=childrenSites;
this.parent=parent;
this.name=name;
this.description=description;
this.namespace=namespace;
}
...
XML mapping as mentioned at the Hibernate site.
Code:
<?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="Site" table="Site">
<id name="id" type="java.lang.Integer" unsaved-value="null">
<generator class="native"/>
</id>
<version name="version" type="long"/>
<set name="childrenSites" inverse="true" lazy="true" cascade="save-update">
<key column="parent"/>
<one-to-many class="Site"/>
</set>
<many-to-one name="parent" column="parent" cascade="save-update" class="Site"/>
<property name="description" type="java.lang.String"/>
<property name="namespace" type="java.lang.String"/>
<property name="name" type="java.lang.String"/>
</class>
</hibernate-mapping>