【问题标题】:NHibernate Criteria Query - Detached query and subquery: System.FormatException: Input string was not in a correct formatNHibernate Criteria Query - 分离查询和子查询:System.FormatException:输入字符串格式不正确
【发布时间】:2021-09-15 22:03:47
【问题描述】:

我正在学习 NHibernate Criteria Queries,我有一个问题希望你们能帮助我。

我有这个模型:

public class Cat
{
    public virtual string Id { get; set; } = string.Empty;
    public virtual string Name { get; set; } = string.Empty;
    public virtual char Sex { get; set; }
    public virtual float Weight { get; set; }
    public virtual CatStore? CatStore { get; set; }
    public virtual DateTime Version { get; set; }
}

然后我运行此查询,但它导致var cats... 行出现异常:

var avgWeight = DetachedCriteria
    .For<Cat>()
    .SetProjection(Projections.Avg("Weight"));
using var session = NHibernateHelper.OpenSession();
var cats = session
    .CreateCriteria<Cat>()
    .Add(Subqueries.Gt("Weight", avgWeight))
    .List();

例外:

  Message: 
    NHibernate.Exceptions.GenericADOException : could not execute query
    [ SELECT this_.CatId as catid1_3_0_, this_.Version as version2_3_0_, this_.Name as name3_3_0_, this_.Sex as sex4_3_0_, this_.Weight as weight5_3_0_, this_.CatStoreId as catstoreid6_3_0_ FROM Cat this_ WHERE ? > (SELECT avg(cast(this_0_.Weight as FLOAT(53))) as y0_ FROM Cat this_0_) ]
      Name:cp0 - Value:Weight
    [SQL: SELECT this_.CatId as catid1_3_0_, this_.Version as version2_3_0_, this_.Name as name3_3_0_, this_.Sex as sex4_3_0_, this_.Weight as weight5_3_0_, this_.CatStoreId as catstoreid6_3_0_ FROM Cat this_ WHERE ? > (SELECT avg(cast(this_0_.Weight as FLOAT(53))) as y0_ FROM Cat this_0_)]
      ----> System.FormatException : Input string was not in a correct format.
  Stack Trace: 
    Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
    CriteriaLoaderExtensions.LoadAllToList[T](IList`1 loaders, ISessionImplementor session)
    SessionImpl.List[T](CriteriaImpl criteria)
    CriteriaImpl.List[T]()
    CriteriaImpl.List()
    CatStoreTests.DetachQueryAsSubqueryTest() line 284
    --FormatException
    Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
    IConvertible.ToDouble(IFormatProvider provider)
    Convert.ToDouble(Object value)
    DoubleType.Set(DbCommand st, Object value, Int32 index, ISessionImplementor session)
    NullableType.NullSafeSet(DbCommand st, Object value, Int32 index, ISessionImplementor session)
    CriteriaNamedParameterSpecification.Bind(DbCommand command, IList`1 multiSqlQueryParametersList, Int32 singleSqlParametersOffset, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
    CriteriaNamedParameterSpecification.Bind(DbCommand command, IList`1 sqlQueryParametersList, QueryParameters queryParameters, ISessionImplementor session)
    SqlCommandImpl.Bind(DbCommand command, ISessionImplementor session)
    Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session)
    Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
    Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)
    Loader.DoList(ISessionImplementor session, QueryParameters queryParameters, IResultTransformer forcedResultTransformer, QueryCacheResultBuilder queryCacheResultBuilder)

我在异常中复制生成的查询,将?替换为Weight即可执行成功针对数据库。

你们能告诉我我在这里做错了什么吗?

【问题讨论】:

  • 您需要提供完整的异常堆栈跟踪
  • 嗨@RomanArtiukhin,我刚刚添加了异常堆栈跟踪

标签: nhibernate


【解决方案1】:

Subqueries.Gt 用于将子查询结果与Subqueries.Gt(11.5, avgSubquery) 等参数值进行比较。尝试将Weight 字符串转换为双精度参数值时抛出异常。

您需要改用Subqueries.PropertyGt

.Add(Subqueries.PropertyGt("Weight", avgWeight))

【讨论】:

猜你喜欢
  • 2014-05-08
  • 2023-03-14
  • 2011-11-23
  • 2022-06-24
  • 2017-03-28
  • 2011-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多