-->
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: Unable to cast object of type
PostPosted: Sat Jul 05, 2008 10:27 pm 
Newbie

Joined: Mon Jul 16, 2007 5:38 pm
Posts: 9
I am having problems trying to use the CreateSqlQuery method.

The error I am getting is:

[NHibernate.MappingException] = {"Invalid mapping information specified for type SchoolAdministration.Business.Student, check your mapping file for property type mismatches"}

InnerException = {"Unable to cast object of type 'CProxyTypeSchoolAdministration_BusinessStudentClassBusiness_NHibernate_ProxyINHibernateProxy1' to type 'System.Collections.Generic.IList`1[SchoolAdministration.Business.StudentClass]'."}

Here is what I am trying:

string q = @"SELECT {c.*}, {s.*}, {sc.*}
FROM Student s
inner join StudentClasses sc on s.id = sc.StudentId
inner join Class c on sc.ClassId=c.Id
WHERE s.Id=:StudentId and sc.SchoolYearId = :SchoolYearId";

IQuery query = session.CreateSQLQuery(q).AddEntity("s", typeof(Student))
.AddEntity("sc", typeof(StudentClass))
.AddEntity("c", typeof(Class))
.AddJoin("c", "s.Classes"); //s.Classes is a List<StudentClass>

query.SetParameter("StudentId", "1");
query.SetParameter("SchoolYearId", "2");

List<Student> ss = query.List<Student>() as List<Student>;

Here is my hbm file:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property">
<class name ="SchoolAdministration.Business.Class, SchoolTest" table="Class">
<id name="Id" column="Id" type="int">
<generator class="native"/>
</id>
<property name="Name" column="Name" type="string"/>
</class>

<class name ="SchoolAdministration.Business.SchoolYear, SchoolTest"
table="SchoolYear">
<id name="Id" column="Id">
<generator class="native"/>
</id>
<property name="Year" column="Year"/>
<property name="Notes" column="Notes"/>
</class>

<class name ="SchoolAdministration.Business.StudentClass, SchoolTest"
table="StudentClass">
<id name="Id" column="Id">
<generator class="native"/>
</id>
<one-to-one name ="Student" class ="SchoolAdministration.Business.Student, SchoolTest"/>
<one-to-one name ="Class" class ="SchoolAdministration.Business.Class, SchoolTest"/>
<one-to-one name ="Year" class ="SchoolAdministration.Business.SchoolYear, SchoolTest"/>
<!--<property name="Grade" column="Grade"/>-->
</class>

<class name ="SchoolAdministration.Business.Student, SchoolTest" table="Student">
<id name="Id" column="Id" unsaved-value="-1" type="int">
<generator class="native"/>
</id>
<many-to-one class="SchoolAdministration.Business.StudentClass, SchoolTest"
column="Id" fetch ="join" name="Classes"/>
<property name ="Name" column ="Name" type="string"/>
</class>
</hibernate-mapping>



Can someone help me please.


Top
 Profile  
 
 Post subject: Re: Unable to cast object of type
PostPosted: Sat Dec 20, 2008 5:07 pm 
Newbie

Joined: Sat Dec 20, 2008 5:01 pm
Posts: 3
I've got the error on SQL Server 2005+, however the same code works fine on SQL Server 2K.

Any chance you've solved the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 4:33 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I don't know what your concrete problem is, but the query in the original post:

string q = @"SELECT {c.*}, {s.*}, {sc.*} ...

returns an array for each row containing a Class object, a Student object and a list of StudenClass. That's the reason why query.List<Student>() isn't working.

Post your query, otherwise it's hard to tell what's going wrong.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 6:22 pm 
Newbie

Joined: Sat Dec 20, 2008 5:01 pm
Posts: 3
wolli wrote:
Post your query, otherwise it's hard to tell what's going wrong.



Thanks for the promp reply.
In my case there is no explicit query. I have two connected entities.
Code:
   <class name="ServiceGroup" lazy="true" table="ServiceGroup">
      <id name="Id" unsaved-value="0" type="Int32" column="ID">
         <generator class="native"/>
      </id>

      <bag name="Services" lazy="true" table="Service" cascade="all-delete-orphan" fetch="subselect" inverse="true" generic="true" order-by="Name asc">
         <key column="Ref_GroupID" />
         <one-to-many class="BaseService" />
      </bag>
                ........
   </class>

   <class name="BaseService" polymorphism="explicit" lazy="true" table="Service">
           <many-to-one name="Group" column="Ref_GroupID" class="ServiceGroup" />

           <many-to-one name="Product" column="Ref_ProductID" class="BaseProduct" />
           <!-- BaseProduct is base entity -->

      <joined-subclass ........ />
      <joined-subclass ........ />
      <joined-subclass ........ />
</class>


I got next error when I was trying to traverse ServiceGroup.Services:

System.InvalidCastException: Unable to cast object of type 'CProxyTypesampai_ProductBaseServiceProduct_NHibernate_ProxyINHibernateProxy_System_Runtime_SerializationISerializable2' to type 'Service'.
In debugger I saw that some objects of the collection were of the right type, but some was of the type noticed in the exception message.

The problem appears on SQL Server 2005, 2008 while on SQL 2K works fine.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 10:02 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Suppose that's your problem:

http://nhforge.org/doc/nh/en/index.html#performance-fetching-proxies

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 26, 2008 6:39 pm 
Newbie

Joined: Sat Dec 20, 2008 5:01 pm
Posts: 3
wolli wrote:


yes, that's definitely my case.

Thank you!


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.