Castle ActiveRecord:'System.Int32'无法转换为'System.Boolean'类型

时间:2011-06-27 15:54:45

标签: sql-server nhibernate castle-activerecord

我在使用已添加到Castle的ActiveRecord较新版本的SqlNamedQuery属性时遇到了一些问题。我已经指定了这个:

[assembly: SqlNamedQuery(Queries.GetItemName, "EXEC [dbo].[GetItemName] :id")]

除此之外,我在结果映射中声明了一个属性:

...
[Property(Access = PropertyAccess.AutomaticProperty, NotNull = true)]
public virtual bool IsPrimaryName { get; set; }
...

Nhibernate查询:

IQuery query = Session.GetNamedQuery(Queries.GetItemName);
query.SetParameter("id", 1212, NHibernateUtil.Int64);
query.SetResultTransformer(Transformers.AliasToBean<Mapping>());

执行后,我收到以下异常:

Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.

我还添加了下面的web.config条目但没有成功(可能它只适用于hql查询)。

<add key="query.substitutions" value="true 1, false 0, yes 'Y', no 'N'" />

所以,我的问题是 - 如何解决这个问题?存储过程返回0或1,但我也尝试使用'1','true','TRUE'等。我怎样才能解决这个问题?或者我可能只是删除SqlNamedQueries?

更新:堆栈跟踪

[ArgumentException: Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.]
System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) +4070954
System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) +9631414
System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) +151
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +223
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28
System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) +101
System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) +25
NHibernate.Properties.BasicSetter.Set(Object target, Object value) +68

更新:选择声明:

SELECT  
        itm.ItemId AS ItemId,
        itm.Value AS Value,
        itm.Id AS ParentId,
        1 AS IsPrimaryName
FROM    
        [dbo].[Item] AS itm
...

正如我上面提到的,我在sp方面尝试了几种不同的场景,包括:'1','true','TRUE'+声明一个BIT变量并直接从select语句返回但是它都失败了有类似的例外。

THX!

3 个答案:

答案 0 :(得分:4)

查询替换用于将HQL文字映射到SQL,因此它们在这里没有帮助。

你的堆栈跟踪是不完整的(你吃了内部异常)所以我不能确定,但​​我猜错误是GetItemName返回int列,你试图将其映射到bool属性。

你可以发布SP的相关SELECT和你的DTO课Mapping(它 IS 是DTO而不是映射的实体,对吗?)


更新:您的SELECT语句需要强制转换

SELECT ...
       CAST(1 as bit) IsPrimaryName
FROM ...

答案 1 :(得分:0)

只要您确定存储过程会为即席查询带来额外好处,命名查询就可以了。 Query.substitution参数正在转换SQL查询的数据,而不是返回。你能告诉我看电话堆吗?

答案 2 :(得分:0)

SQL Server支持布尔类型IIRC,那么为什么不重写存储过程“GetItemName”来返回布尔值呢?

否则我猜你需要修改Dialect以支持1/0替代true / false。