gpt4 book ai didi

mysql - NHibernate 和 MySQL Nullable 对象必须有一个值

转载 作者:行者123 更新时间:2023-11-29 14:56:36 25 4
gpt4 key购买 nike

我遇到了一个问题,希望得到一些关于从哪里开始的指导。让我先介绍一下场景。

我最近从 SQL Server 迁移到 MySQl 数据库,所有针对 SQL Server 通过的集成测试现在都失败了,因为迁移出现了以下异常:

NHibernate.Exceptions.GenericADOException : could not execute query
[ select post0_.Id as Id1_, post0_.CreatedAt as CreatedAt1_, post0_.Title as Title1_, post0_.Body as Body1_, post0_.CreatedBy as CreatedBy1_, post0_.ParentId as ParentId1_ from Posts post0_, Users user1_ where post0_.CreatedBy=user1_.Id and ((user1_.Username is null) and (? is null) or user1_.Username=?) and (post0_.ParentId is null) order by post0_.CreatedAt desc ]
Name:p1 - Value:username1
[SQL: select post0_.Id as Id1_, post0_.CreatedAt as CreatedAt1_, post0_.Title as Title1_, post0_.Body as Body1_, post0_.CreatedBy as CreatedBy1_, post0_.ParentId as ParentId1_ from Posts post0_, Users user1_ where post0_.CreatedBy=user1_.Id and ((user1_.Username is null) and (? is null) or user1_.Username=?) and (post0_.ParentId is null) order by post0_.CreatedAt desc]
----> System.InvalidOperationException : Nullable object must have a value

.

我使用带有 POCO 模型和手工 SQL 的 NHibernate v3(使用 DBDeploy.NET 管理数据库版本控制)

有可能,而且最有可能的是,我在将架构创建 SQL 从 SQL Server 转换为 MySQL 时犯了一个错误,但令我困惑的是,即使查询结果没有记录,我也会收到此错误。

为什么 NHibernate 试图创建任何对象?我不明白它是如何工作的?

这是其中一个查询的示例。返回行时这会引发异常。根据设计,唯一返回 null 的列是 Post.ParentId 列。

return GetSession()
.Query<Post>()
.Where(p => p.CreatedBy.Username == username && p.Parent == null)
.OrderByDescending(p => p.CreatedAt)
.Take(count)
.ToList();

map 文件和类没有更改,但已包含它们以供下面引用。

非常欢迎任何我应该关注的想法。

邮政 map

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="clubrd.service" namespace="clubrd.service">
<class name="Post" table="Posts">
<id name="Id">
<generator class="guid.comb"/>
</id>
<property name="CreatedAt"/>
<property name="Title" column="Title"/>
<property name="Body"/>
<many-to-one name="CreatedBy" column="CreatedBy"/>
<many-to-one name="Parent" column="ParentId"/>
<set name="Children">
<key column="ParentId"/>
<one-to-many class="Post"/>
</set>
</class>
</hibernate-mapping>

用户映射,此处引用为 Post.CreatedBy

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="clubrd.service" namespace="clubrd.service">
<class name="User" table="Users">
<id name="Id">
<generator class="guid.comb"/>
</id>
<property name="Username" not-null="true"/>
<property name="Email" not-null="true"/>
<property name="PasswordHash"/>
<property name="PasswordSalt"/>
<one-to-one name="Profile" class="Profile" />
</class>
</hibernate-mapping>

Post 类

public class Post : Entity
{
public virtual string Title { get; set; }
public virtual string Body { get; set; }
public virtual Post Parent { get; set; }
public virtual ICollection<Post> Children { get; set; }
}

用户类

public class User : Entity
{
public virtual string Name { get { return this.Username; } }
public virtual string Username { get; set; }
public virtual string PasswordHash { get; set; }
public virtual string PasswordSalt { get; set; }
public virtual string Email { get; set; }

public virtual Profile Profile { get; set; }
public virtual ICollection<ObjectRole> Roles { get; set; }
}

编辑错误点的完整输出以及前面显示正在构造的实体的 NHibernate 日志。

NHibernate.Cfg.SettingsFactory:  2010-12-18 15:56:20,159 INFO Default entity-mode: Poco
NHibernate.Cfg.SettingsFactory: 2010-12-18 15:56:20,160 INFO Named query checking : enabled
NHibernate.Impl.SessionFactoryImpl: 2010-12-18 15:56:20,211 INFO building session factory
NHibernate.Impl.SessionFactoryObjectFactory: 2010-12-18 15:56:20,580 INFO no name configured
NHibernate.Hql.Ast.ANTLR.Tree.FromElement: 2010-12-18 15:56:21,389 INFO handling property dereference [clubrd.service.Post (p) -> CreatedBy (Class)]
NHibernate.Hql.Ast.ANTLR.Tree.FromElement: 2010-12-18 15:56:21,406 INFO handling property dereference [clubrd.service.User () -> Username (Class)]
NHibernate.Hql.Ast.ANTLR.Tree.FromElement: 2010-12-18 15:56:21,416 INFO handling property dereference [clubrd.service.Post (p) -> CreatedBy (Class)]
NHibernate.Hql.Ast.ANTLR.Tree.FromElement: 2010-12-18 15:56:21,417 INFO handling property dereference [clubrd.service.User () -> Username (Class)]
NHibernate.Hql.Ast.ANTLR.Tree.FromElement: 2010-12-18 15:56:21,419 INFO handling property dereference [clubrd.service.Post (p) -> Parent (Class)]
NHibernate.Hql.Ast.ANTLR.Tree.FromElement: 2010-12-18 15:56:21,424 INFO handling property dereference [clubrd.service.Post (p) -> CreatedAt (Class)]
NHibernate.Util.ADOExceptionReporter: 2010-12-18 15:56:21,564 WARN System.InvalidOperationException: Nullable object must have a value.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Nullable`1.get_Value()
at NHibernate.Dialect.MySQLDialect.GetLimitString(SqlString querySqlString, Int32 offset, Int32 limit, Nullable`1 offsetParameterIndex, Nullable`1 limitParameterIndex) in d:\CSharp\NH\nhibernate\src\NHibernate\Dialect\MySQLDialect.cs:line 197
at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1119
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 399
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 236
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1665
NHibernate.Util.ADOExceptionReporter: 2010-12-18 15:56:21,576 ERROR Nullable object must have a value.
NHibernate.Util.ADOExceptionReporter: 2010-12-18 15:56:21,577 WARN System.InvalidOperationException: Nullable object must have a value.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Nullable`1.get_Value()
at NHibernate.Dialect.MySQLDialect.GetLimitString(SqlString querySqlString, Int32 offset, Int32 limit, Nullable`1 offsetParameterIndex, Nullable`1 limitParameterIndex) in d:\CSharp\NH\nhibernate\src\NHibernate\Dialect\MySQLDialect.cs:line 197
at NHibernate.Loader.Loader.PrepareQueryCommand(QueryParameters queryParameters, Boolean scroll, ISessionImplementor session) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1119
at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 399
at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 236
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs:line 1665
NHibernate.Util.ADOExceptionReporter: 2010-12-18 15:56:21,579 ERROR Nullable object must have a value.

NHibernate.Exceptions.GenericADOException : could not execute query
[ select post0_.Id as Id1_, post0_.CreatedAt as CreatedAt1_, post0_.Title as Title1_, post0_.Body as Body1_, post0_.CreatedBy as CreatedBy1_, post0_.ParentId as ParentId1_ from Posts post0_, Users user1_ where post0_.CreatedBy=user1_.Id and ((user1_.Username is null) and (? is null) or user1_.Username=?) and (post0_.ParentId is null) order by post0_.CreatedAt desc ]
Name:p1 - Value:username1
[SQL: select post0_.Id as Id1_, post0_.CreatedAt as CreatedAt1_, post0_.Title as Title1_, post0_.Body as Body1_, post0_.CreatedBy as CreatedBy1_, post0_.ParentId as ParentId1_ from Posts post0_, Users user1_ where post0_.CreatedBy=user1_.Id and ((user1_.Username is null) and (? is null) or user1_.Username=?) and (post0_.ParentId is null) order by post0_.CreatedAt desc]
----> System.InvalidOperationException : Nullable object must have a value.
at NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs: line 1674
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs: line 1589
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes) in d:\CSharp\NH\nhibernate\src\NHibernate\Loader\Loader.cs: line 1583
at NHibernate.Hql.Ast.ANTLR.Loader.QueryLoader.List(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\Loader\QueryLoader.cs: line 298
at NHibernate.Hql.Ast.ANTLR.QueryTranslatorImpl.List(ISessionImplementor session, QueryParameters queryParameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Hql\Ast\ANTLR\QueryTranslatorImpl.cs: line 110
at NHibernate.Engine.Query.HQLQueryPlan.PerformList(QueryParameters queryParameters, ISessionImplementor session, IList results) in d:\CSharp\NH\nhibernate\src\NHibernate\Engine\Query\HQLQueryPlan.cs: line 105
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters queryParameters, IList results) in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs: line 658
at NHibernate.Impl.SessionImpl.List(IQueryExpression queryExpression, QueryParameters parameters) in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs: line 634
at NHibernate.Impl.ExpressionQueryImpl.List() in d:\CSharp\NH\nhibernate\src\NHibernate\Impl\ExpressionQueryImpl.cs: line 63
at NHibernate.Linq.NhQueryProvider.ExecuteQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery) in d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 78
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 27
at NHibernate.Linq.NhQueryProvider.Execute(Expression expression) in d:\CSharp\NH\nhibernate\src\NHibernate\Linq\NhQueryProvider.cs: line 102
at Remotion.Data.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList(IEnumerable`1 source)
at clubrd.data.Repository.PostRepository.UsersLatestPosts(String username, Int32 count) in PostRepository.cs: line 13
at clubrd.data.integrationtest.Repository.PostRepositoryTests.UsersPosts() in PostRepositoryTests.cs: line 103
--InvalidOperationException

最佳答案

感谢 Mauricio 为我指明了堆栈跟踪。这似乎是 MySQL5Dialect 的 GetLimitString() 实现的问题。将方言从 MySQL5Dialect 更改为 MySQLDialect 似乎可以解决该问题。

虽然,也许我累了,但是当我查看代码时,MySQL 版本似乎包含此异常的风险,而不是 MySQL5 版本。

MySQL方言

public override SqlString GetLimitString(SqlString querySqlString, int offset, int limit, int? offsetParameterIndex, int? limitParameterIndex)
{
var pagingBuilder = new SqlStringBuilder();
pagingBuilder.Add(querySqlString);
pagingBuilder.Add(" limit ");

if (offset > 0)
{
pagingBuilder.Add(Parameter.WithIndex(offsetParameterIndex.Value));
pagingBuilder.Add(", ");
}

pagingBuilder.Add(Parameter.WithIndex(limitParameterIndex.Value));

return pagingBuilder.ToSqlString();
}

MySQL5方言

public override SqlString GetLimitString(SqlString querySqlString, int offset, int limit)
{
var pagingBuilder = new SqlStringBuilder();

pagingBuilder.Add(querySqlString);
pagingBuilder.Add(" limit ");
if (offset > 0)
{
pagingBuilder.Add(offset.ToString());
pagingBuilder.Add(", ");
}

pagingBuilder.Add(limit.ToString());

return pagingBuilder.ToSqlString();
}

关于mysql - NHibernate 和 MySQL Nullable 对象必须有一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4479002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com