gpt4 book ai didi

NHibernate.TypeMismatchException : Provided id of the wrong type. 预期 : System. Int32,得到 System.Int64

转载 作者:行者123 更新时间:2023-12-02 18:50:09 27 4
gpt4 key购买 nike

我使用以下查询来获取客户端。客户端有一个 long 类型的公共(public) ID。

var client = Session.CreateQuery("from Client as c where c.Id = :Id").SetParameter("Id", 1, NHibernateUtil.Int64).UniqueResult<Client>();

我收到错误:

NHibernate.TypeMismatchException: Provided id of the wrong type. Expected: System.Int32, got System.Int64

同时,以下内容也可以正常工作。

var client = Session.Get<Client>(1L); //Or
var client = Session.CreateCriteria<Client>().Add(Restrictions.Eq("Id", 1L)).UniqueResult<Client>();

我错过了什么?我使用 Fluent nhibernate 来创建映射。我已经针对 Sqlite 和 MySql 数据库测试了查询。结果相同。

Edit1:从映射生成的模式显然是使用 bigint 作为 mysql 上的主键。这就是为什么我无法理解 Int32 期待什么?

Edit2:好的,我的 Client 类具有对 Report 对象的引用。它实际上是数据库中与具有列 clientID 的报告表的一对一关系。 Report 类有一个 int 类型的 id。一旦我将其类型更改为 long,错误就消失了。

我的映射如下:

客户 map :

HasOne<Report>(x => x.Report)
.PropertyRef(x => x.Client)
.LazyLoad()
.Cascade.SaveUpdate();

报告图:

References(x => x.Client, "clientID").Unique();

那么,为什么通过将reportid的类型从int改为long,问题就自行解决了。其次,当我不要求报告时,为什么还要费心去获取报告?

最佳答案

一对一键需要具有相同的类型定义,因为它们使用相同的值。因此,ID = Int32.MaxValue + 1 的客户端将有一个 ID = Int32.MaxValue + 1 的相应报告,因此它们必须都是long

我推荐这些文章来了解一对一以及大多数时候您并不真正需要它:

关于NHibernate.TypeMismatchException : Provided id of the wrong type. 预期 : System. Int32,得到 System.Int64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2055259/

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