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
|