-->
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.  [ 6 posts ] 
Author Message
 Post subject: System.InvalidCastException: Specified cast is not valid.
PostPosted: Fri Dec 02, 2005 12:36 am 
Newbie

Joined: Fri Dec 02, 2005 12:11 am
Posts: 3
New and need help with this error: System.InvalidCastException: Specified cast is not valid. I'm using an Access database and I have just two tables (one-to-many and many-to-one).

Source Line getting error:
IList deptList = mSession.CreateQuery( "from Department Department join fetch Department.Classes" ).List();

Class 1 and hbm file:
public class Department
{
private int mDeptID;
private string mDeptName;

//private IDictionary mClasses;
private IList mClasses = new ArrayList(); // One-To-Many Relationship

public int DeptID
{
get { return mDeptID; }
set { mDeptID = value; }
}

public string DeptName
{
get { return mDeptName; }
set { mDeptName = value; }
}

public IList Classes
//public IDictionary Classes
{
get { return mClasses; }
set { mClasses = value; }
}
}

} // end namespace yyyy

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernate.Examples.QuickStart.Department, NHibernate.Examples" table="Department">
<id name="DeptID">
<generator class="assigned" />
</id>
<property name="DeptName"/>
<set name="Classes" cascade="all" lazy="true">
<key column="DeptID"/>
<one-to-many class="NHibernate.Examples.QuickStart.UniversityClass, NHibernate.Examples"/>
</set>
</class>
</hibernate-mapping>

Class 2 and hbm file:
public class UniversityClass
{
private int mClassID;
private string mClassName;
private int mDeptID;
private Department mDepartment = new Department();

public UniversityClass()
{
}

public int ClassID
{
get { return mClassID; }
set { mClassID = value; }
}

public string ClassName
{
get { return mClassName; }
set { mClassName = value; }
}

public int DeptID
{
get { return mDeptID; }
set { mDeptID = value; }
}

public Department Department
{
get { return mDepartment; }
set { mDepartment = value; }
}

}

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernate.Examples.QuickStart.UniversityClass, NHibernate.Examples" table="UniversityClass">
<id name="ClassID">
<generator class="assigned" />
</id>
<property name="ClassName"/>
<many-to-one name="Department"
class="NHibernate.Examples.QuickStart.Department, NHibernate.Examples"
column="DeptID"/>
</class>
</hibernate-mapping>

App config file:
<nhibernate>
<add
key="hibernate.show_sql"
value="true"
/>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>

<add
key="hibernate.dialect"
value="NHibernate.JetDriver.JetDialect, NHibernate.JetDriver"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.JetDriver.JetDriver, NHibernate.JetDriver"
/>

<add
key="hibernate.connection.connection_string"
value="Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=D:\My Documents\Visual Studio.Net\C#\nhibernateTest\NHibernate.mdb"
/>

</nhibernate>

Log file:
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Loader.Loader [(null)] <(null)> - Initializing object from DataReader: 4
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Loader.Loader [(null)] <(null)> - Hydrating entity: NHibernate.Examples.QuickStart.UniversityClass#4
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Type.StringType [(null)] <(null)> - returning 'English(D2)' as column: ClassName1_
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Type.Int32Type [(null)] <(null)> - returning '2' as column: DeptID1_
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Type.Int32Type [(null)] <(null)> - returning '2' as column: DeptID__
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Loader.Loader [(null)] <(null)> - found row of collection: [NHibernate.Examples.QuickStart.Department.Classes#2]
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - reading row
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Type.Int32Type [(null)] <(null)> - returning '4' as column: ClassID__
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - loading [UniversityClass#4]
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - attempting to resolve [UniversityClass#4]
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - resolved object in session cache [NHibernate.Examples.QuickStart.UniversityClass#4]
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Loader.Loader [(null)] <(null)> - done processing result set (4 rows)
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Driver.NHybridDataReader [(null)] <(null)> - running NHybridDataReader.Dispose()
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed Reader, open Readers :0
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.BatcherImpl [(null)] <(null)> - Closed IDbCommand, open IDbCommands :0
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Loader.Loader [(null)] <(null)> - total objects hydrated: 6
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - resolving associations for: [NHibernate.Examples.QuickStart.Department#1]
2005-12-01 23:24:42,000 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - returning loading collection:[NHibernate.Examples.QuickStart.Department.Classes#1]
2005-12-01 23:24:42,781 [3244] ERROR NHibernate.ADOException [(null)] <(null)> - Could not execute query
System.InvalidCastException: Specified cast is not valid.
at NHibernate.Persister.GetSetHelper_NHibernate_Examples_QuickStart_Department.SetPropertyValues(Object obj, Object[] values)
at NHibernate.Persister.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values)
at NHibernate.Impl.SessionImpl.InitializeEntity(Object obj)
at NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session)
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Object optionalObject, Object optionalId, Object[] optionalCollectionKeys, Boolean returnProxies)
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes)
at NHibernate.Hql.QueryTranslator.List(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters parameters)
2005-12-01 23:24:44,406 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - closing session
2005-12-01 23:24:44,406 [3244] DEBUG NHibernate.Impl.SessionImpl [(null)] <(null)> - disconnecting session
2005-12-01 23:24:44,406 [3244] DEBUG NHibernate.Connection.ConnectionProvider [(null)] <(null)> - Closing connection


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 10:26 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
you didn't say what do you expect this query to return. Because you use a join it will return a list of arrays of Department and Classes objects. Please explain your intention.

cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 02, 2005 8:47 pm 
Newbie

Joined: Fri Dec 02, 2005 12:11 am
Posts: 3
Thanks for your reply.

Because I was experimenting just to gain experience I just wanted it to work. First I executed the below to return all departments

IList deptList = mSession.CreateCriteria(typeof( Department )).List();

And it failed with the same error.

Then (grasping) attempted the command below trying to obtain all Departments with their UniversityClasse(Classes) thinking I needed to join theses tables for this to work and received the same error. The first command below is the one in my original post.

It appears according to the log it did return this result and then the ado.exception occurred followed by the invalid cast message.

IList deptList = mSession.CreateQuery( "from Department Department join fetch Department.Classes" ).List();

IList deptList = mSession.CreateQuery( "from Department d inner join fetch d.Classes as UniversityClass" ).List();

IList deptList = mSession.CreateQuery( "from Department d inner join d.Classes" ).List();


Interesting, when I take out the one-to-many relationship in my mapping file: Department.hbm.xml

The command: IList deptList = mSession.CreateCriteria(typeof( Department )).List();

Does execute successfully returning all Departments.

Specifically, I remove the following from the mapping file

<set name="Classes" cascade="all" lazy="true">
<key column="DeptID"/>
<one-to-many class="NHibernate.Examples.QuickStart.UniversityClass, NHibernate.Examples"/>
</set>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 8:00 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
change the mapping from set to a bag as you implemented it using an IList.

cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 9:27 am 
I also tried going to a bag as below and recieved the same error....I also switched by database from Access to SQL Server 2000 with the same results.

Notice I formatted my post now. Should have done that with my original post.

Code:
<bag name="Classes" cascade="all" lazy="true">
  <key column="DeptID"/>
  <one-to-many class="NHibernate.Examples.QuickStart.UniversityClass,    NHibernate.Examples"/>
</bag>


Top
  
 
 Post subject:
PostPosted: Sat Dec 03, 2005 10:07 am 
Newbie

Joined: Fri Dec 02, 2005 12:11 am
Posts: 3
I found comments from the forum in "The Code Project" (Source of my first Nhibernate project) that has corrected my error.

In my Department class I changed the property representing the many class

From: IList mClasses
To: ISet mClasses

All is fine now, thanks for the assistance.

ISet is located in the Iesi.Collections reference.


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