-->
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.  [ 2 posts ] 
Author Message
 Post subject: ISession.Find throwing "Specified cast is not valid&quo
PostPosted: Thu Feb 09, 2006 6:44 pm 
Newbie

Joined: Thu Feb 09, 2006 6:08 pm
Posts: 1
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:1.1


The test fixture does this:
ISession session = factory.OpenSession();
Language lang = (Language) session.Find("from Language as l where l.Name = ?", "english", NHibernateUtil.String);

Language.cs:
public class Language
{
private int _LanguageId;
private string _Name;
private string _Description;
private bool _Active;

public int LanguageId
{
get { return _LanguageId; }
set { _LanguageId = value; }
}

public string Name
{
get { return _Name; }
set { _Name = value; }
}

public string Description
{
get { return _Description; }
set { _Description = value; }
}

public bool Active
{
get { return _Active; }
set { _Active = value; }
}

}
}

Language.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="ScotiaOCAPDAL.Language, ScotiaOCAPDAL" table="Language">
<id name="LanguageId" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="Name" />
<property name="Description"/>
<property name="Active" />
</class>

</hibernate-mapping>

sql out:
NHibernate: select language0_.LanguageId as LanguageId, language0_.Name as Name, language0_.Description as Descript3_, language0_.Active as Active from Language language0_ where (language0_.Name=@p0)
@p0 = 'english'

The error message in NUnit:
ScotiaOCAPDAL.LanguageFixture.LoadName : System.InvalidCastException : Specified cast is not valid.

What am i doing wrong in the session.Find() step. I get it to work accepting Int32 but can't get it to go for string.

I got it to go with createCriteria, but I want to see why session.Find is failing like this.

Any help much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 7:06 pm 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
to me the generated query looks ok, but I have doubts about the table structure and contents: what type did you declared in the db for the boolean property? the column is nullable and contains null? that could explain the exception. I consider good practice to declare in the mapping for the property the data type. anyway you can't load a null from the database in a boolean property. either declare the column not null or use Nullables.

cheers,
radu


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