-->
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.  [ 3 posts ] 
Author Message
 Post subject: Composite pattern example problem.
PostPosted: Tue May 11, 2004 3:43 pm 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
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>


Thanks in advance,
Stijn.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 5:56 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Hi,

Could you try not to cast the ArrayList like this :
Code:
site.setChildrenSites(l)


Tell me if it is OK...

Charles


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 12, 2004 10:22 am 
Beginner
Beginner

Joined: Tue May 11, 2004 10:40 am
Posts: 37
Location: Belgium
It's already solved.

Thanks anyway...

Greets,
Stijn.


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