-->
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: one-to-many,bidirectionnal : could not save object
PostPosted: Mon Dec 12, 2005 11:26 am 
Newbie

Joined: Tue Nov 15, 2005 2:14 am
Posts: 7
I received a could not save object exception and the InnerException is :
Unable to cast object of type 'System.Collections.ArrayList' to type 'Iesi.Collections.ISet'

Summary of the two classes :

Code:

public class Product
{
      private int id;
      private String name;
      private ArrayList releases; //I also try an ISet but with the same class cast exception
               
                // + properties
}


public class ProductRelease
{
      private int id;
      private String name;
      private DateTime released;
      private Product product;
}


So a product can have more than one release.

The mapping files :

Code:
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="sgub.ProductRelease, sgub" table="productrelease">
      <id name="Id" column="id" >
         <generator class="native" />
      </id>
      <property name="Name" column= "name" type="String" length="45" />
      <property name="Released" column= "productreleased" type="Date" />
      <many-to-one name="Product" column="productid" not-null="true" />
      
   </class>

</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="sgub.Product, sgub" table="product">
      <id name="Id" column="id" >
         <generator class="native" />
      </id>
      <property name="Name" column= "name" type="String" length="45"/>
      <set name="Releases" inverse="true">
         <key column="id" />
         <one-to-many class="sgub.ProductRelease, sgub" />
      </set>
      
   </class>
</hibernate-mapping>


And the way I save the objects
Code:
Product nutella=new Product();
nutella.Name="Nutella";

ProductRelease alpha=new ProductRelease();
alpha.Name="alpha";
alpha.Released=DateTime.Now;
      
nutella.Releases.Add(alpha);
alpha.Product=nutella;

session =factory.OpenSession();
transaction = session.BeginTransaction();
session.Save(nutella);
session.Flush();
transaction.Commit();


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 12:34 pm 
OK I replace ArrayList by ISet but now my program save only the Product object and not the ProductReleases objects :-(

Why?


Top
  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 12:40 pm 
Newbie

Joined: Tue Nov 15, 2005 2:14 am
Posts: 7
Ok I call the save method on all the objects:

Code:
session.Save(nutella);
session.Save(alpha);
session.Save(beta);


Is it an expected behavior? :?:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 1:31 pm 
Newbie

Joined: Wed Dec 07, 2005 12:32 am
Posts: 11
Location: Chennai, India
With the given hbm, yes this is the expected behaviour.

If you want to save the children when saving the parent, then use the cascade attribute in your parent definition. Just for saving and updating alone, use "save-update". In case u want to have this behaviour for save, update and delete, use cascade="all".

_________________
Thanks & Regards,
~Venki


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.