-->
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: error upgrading from NHibernate 1.2.0.3001 to 1.2.0.4000
PostPosted: Fri May 18, 2007 8:28 am 
Newbie

Joined: Fri Jan 19, 2007 1:42 pm
Posts: 6
Ive been using NHibernate 1.2.0.3001 (which is fantastic BTW) and recently tried to upgrade to the 4000 release - however when I did this I received an error similar to the following

PersistanceUpdate.Tests.UserTests.CreateUser : NHibernate.PropertyAccessException : The type NHibernate.Collection.PersistentBag can not be assigned to a property of type System.Collections.Generic.IList`1[PersistanceUpdate.Country] setter of PersistanceUpdate.User.UserCountries
----> System.ArgumentException : Object of type 'NHibernate.Collection.PersistentBag' cannot be converted to type 'System.Collections.Generic.IList`1[PersistanceUpdate.Country]'.

My mapping class for my test User class is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="none" default-lazy="false" schema="Test.dbo">
<class
name="PersistanceUpdate.User,PersistanceUpdate"
table="Users">
<id name="Id" column="UserId" type="int" length="4" unsaved-value="0">
<generator class="identity" />
</id>

<property name="Name" column="UserName"/>

<bag name="UserCountries" table="UserCountries">
<key column="UserId"/>
<many-to-many class="PersistanceUpdate.Country,PersistanceUpdate" column="CountryId" />
</bag>



</class>
</hibernate-mapping>

The UserCountries bag maps to an IList<Country> public setter on the User object

I have no problems with 1.2.0.3001 with this code and nothing else has changed expect the new NHibernate dll ( I double checked an re-referenced the 3001 version dll and it works perfectly)

Any ideas on how to fix this as I would really like to use the current version of NHibernate (apologies if this has been addressed before - I did try and find a resolution in the forums but couldn't see anything that applied, and the online help for v1.2.0 says nothing about not being able to map a bag to an IList<>)

many thanks

W


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 18, 2007 8:44 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Please post the User class code.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 19, 2007 11:59 am 
Regular
Regular

Joined: Wed Apr 25, 2007 4:18 am
Posts: 51
Location: Belarus, Gomel
Hi willardspoon!

Don't you have to add generic="true" to your mapping?

_________________
WBR, Igor


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 21, 2007 5:40 am 
Newbie

Joined: Fri Jan 19, 2007 1:42 pm
Posts: 6
Hi guys - the user class originally inherits from an Entity class which provides the Save functionality. Ive stipped this out into the User class (please note I dont normally use the session in this way but it does replicate the error):

User class:

using System;
using System.Collections.Generic;
using System.Text;
using NHibernate.Cfg;
using NHibernate;

namespace PersistanceUpdate
{
public class User
{
private int _id;
private string _name;
private IList<Country> _userCountries = new List<Country>();
private Configuration cfg = new Configuration().Configure();
private ISessionFactory _factory = null;

public User()
{
_factory = cfg.BuildSessionFactory();
}

public int Id
{
get { return this._id; }
set { this._id = value; }
}

public string Name
{
get { return this._name; }
set { this._name = value; }
}

public IList<Country> UserCountries
{
get { return this._userCountries; }
set { this._userCountries = value; }
}

public void Save()
{
ISession session = _factory.OpenSession();
ITransaction trans = session.BeginTransaction();

try
{
session.Save(this);
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
throw ex;
}
finally
{
if(session != null)
session.Close();
}

}
}
}

And the user mapping class is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="none" default-lazy="false" schema="Test.dbo">
<class
name="PersistanceUpdate.User,PersistanceUpdate"
table="Users">
<id name="Id" column="UserId" type="int" length="4" unsaved-value="0">
<generator class="identity" />
</id>

<property name="Name" column="UserName"/>

<bag name="UserCountries" table="UserCountries" generic="true">
<key column="UserId"/>
<many-to-many class="PersistanceUpdate.Country,PersistanceUpdate" column="CountryId" />
</bag>



</class>
</hibernate-mapping>


I get the error when I run the following test:

User newUser = new User();
newUser.Name = "User Name";
newUser.Save();

Note - if I remove the bag from the mapping file the user saves correctly

The Country mapping file is as follows

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-cascade="none" default-lazy="false" schema="Test.dbo">
<class
name="PersistanceUpdate.Country,PersistanceUpdate"
table="Countries" >
<id name="Id" column="CountryId" type="int" length="4" unsaved-value="0">
<generator class="identity" />
</id>

<property name="Name" column="CountryName"/>



</class>
</hibernate-mapping>

My hibernate.cfg.xml file looks like:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory >
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">{My Connection String}</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>

As a sanity check I left eveything the same but changed the NHibernate dll back to v1.2.0.3001 and then save worked correctly

Any help would be much appreciated

many thanks

W


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.