-->
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.  [ 5 posts ] 
Author Message
 Post subject: Specified cast is not valid
PostPosted: Tue Mar 21, 2006 4:14 pm 
Newbie

Joined: Tue Mar 21, 2006 3:52 pm
Posts: 5
Location: Atlanta, GA
Hibernate version: 1.0.2.0.

Mapping documents:

Person.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernateWeb.Person, NHibernateWeb" table="People">
<id name="ID" column="PersonID" type="Int32">
<generator class="assigned" />
</id>
<discriminator column="PersonType" type="String" />
<property name="FirstName" column="FirstName" />
<property name="LastName" column="LastName" />
<subclass name="NHibernateWeb.Professor, NHibernateWeb" discriminator-value="Professor">
<property name="Identifier" column="Identifier" type="String"/>
</subclass>
<subclass name="NHibernateWeb.Student, NHibernateWeb" discriminator-value="Student">
<property name="SSN" column="Identifier" type="String"/>
</subclass>
</class>
</hibernate-mapping>


Department.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="NHibernateWeb" assembly="NHibernateWeb" >
<class name="Department" table="Department">
<id name="ID" column="DeptID" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" column="DeptName" />
<set name="Classes" cascade="all">
<key column="DeptID" />
<one-to-many class="UniversityClass" />
</set>
<set name="Professors" table="DepartmentProfessor">
<key column="DeptID" />
<many-to-many class="Person" column="PersonID" />
</set>
</class>
</hibernate-mapping>


UniversityClass.hbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="NHibernateWeb" assembly="NHibernateWeb">
<class name="UniversityClass" table="UniversityClass">
<id name="ID" column="ClassID" type="Int32" unsaved-value="0">
<generator class="assigned" />
</id>
<property name="Name" column="ClassName" />
<property name="Number" column="ClassNumber" />
<property name="Syllabus" column="Syllabus" />
<property name="StartDate" column="StartDate" />
<many-to-one name="Department" class="Department" column="DeptID" not-null="true" />
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

public System.Collections.IList getDepartments()
{
IList depts = null;

ISession session = factory.OpenSession();
ITransaction tx = session.BeginTransaction();
try
{
depts = session.CreateCriteria(typeof(Department)).List();
}
catch (Exception ex)
{
throw ex;
tx.Rollback();
// handle exception.
}
finally
{
session.Close();
}
return depts;
}






Full stack trace of any exception that occurs:

[InvalidCastException: Specified cast is not valid.]
NHibernate.Persister.GetSetHelper_NHibernateWeb_Department.SetPropertyValues(Object obj, Object[] values)

[MappingException: Invalid mapping information specified for type NHibernateWeb.Department, check your mapping file for property type mismatches]
NHibernateWeb.RegMgr.getDepartments() in c:\inetpub\wwwroot\nhibernateweb\regmgr.cs:40
NHibernateWeb.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\nhibernateweb\webform1.aspx.cs:26
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()






Name and version of the database you are using:

SQL Server 2000




The generated SQL (show_sql=true):


I couldn't figured out how to do this.



Debug level Hibernate log excerpt:

I don't know what this is either.




I keep getting this error when I run my code, but I wrote this from an example site. I need a second pair of eyes on this cause the mappings look ok to me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 21, 2006 6:32 pm 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
How does Your department class look like? Do You use generic lists? AFAIK, they are not yet supported by NHibernate.

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 10:35 am 
Newbie

Joined: Tue Mar 21, 2006 3:52 pm
Posts: 5
Location: Atlanta, GA
gert wrote:
How does Your department class look like? Do You use generic lists? AFAIK, they are not yet supported by NHibernate.

Gert


Here is the Department class.


using System;
using System.Collections;

namespace NHibernateWeb
{
/// <summary>
/// Summary description for Department.
/// </summary>
public class Department
{
private int id;
private string name;
private IDictionary classes;
private IDictionary professors;

public int ID
{
get {return this.id;}
set {this.id = value;}
}

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

public IDictionary Classes
{
get {return this.classes;}
set {this.classes = value;}
}

public IDictionary Professors
{
get {return this.professors;}
set {this.professors = value;}
}

}
}


Sorry for my ignorance, but I am new to NHibernate. I am trying it out because I gave up my attempt to learn another popular O/R Mapper.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 10:40 am 
Expert
Expert

Joined: Thu Jan 19, 2006 4:29 pm
Posts: 348
Quote:
Code:
<set name="Classes" cascade="all">



If You use Set mapping, the field (and property) should be declared as either IEsi.Collections.ISet or ICollection. <Set> is not assignable to IDictionary

Gert


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 22, 2006 11:02 am 
Newbie

Joined: Tue Mar 21, 2006 3:52 pm
Posts: 5
Location: Atlanta, GA
gert wrote:
Quote:
Code:
<set name="Classes" cascade="all">



If You use Set mapping, the field (and property) should be declared as either IEsi.Collections.ISet or ICollection. <Set> is not assignable to IDictionary

Gert


That worked. I read the docs and I still don't quite understand why it worked, but it worked.

Thank you for your help.


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