流利的 NHibernate 和 MySQL 转换语法?
我在 MySQL 中有一个表,其中有一个形式为“Xnnnn”的字符串列“Id”,其中 nnnn 是一个数字。
我想找到定义的最大的nnnn。所以我有:
var c = s.CreateCriteria(typeof(Item))
.AddOrder(Order.Desc(
Projections.Cast(
NHibernateUtil.Int32,
Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property("Id"),
Projections.Constant(2), Projections.Constant(10)))
))
.SetProjection(Projections.Property("Id"))
.SetMaxResults(1)
.List<string>();
但是 NHibernate 生成 SQL:
SELECT this_.Id as y0_ FROM `Item` this_
ORDER BY cast(substring(this_.Id, ?p0, ?p1) as INTEGER) desc limit ?p2
MySQL 不喜欢...它坚持:
cast(substring(this_.Id, ?p0, ?p1) as SIGNED INTEGER)
我已经尝试了各种类型的转换,但没有一个产生正确的输出。我需要做的所有其他映射工作都很好。
有什么建议吗?
I have a table in MySQL which has a string column 'Id' of the form "Xnnnn" where nnnn is a number.
I want to find the largest nnnn defined. So I have:
var c = s.CreateCriteria(typeof(Item))
.AddOrder(Order.Desc(
Projections.Cast(
NHibernateUtil.Int32,
Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property("Id"),
Projections.Constant(2), Projections.Constant(10)))
))
.SetProjection(Projections.Property("Id"))
.SetMaxResults(1)
.List<string>();
But NHibernate generates the SQL:
SELECT this_.Id as y0_ FROM `Item` this_
ORDER BY cast(substring(this_.Id, ?p0, ?p1) as INTEGER) desc limit ?p2
Which MySQL doesn't like ... it insists on:
cast(substring(this_.Id, ?p0, ?p1) as SIGNED INTEGER)
I've tried various types in the cast and none of them produce the right output. Everything else I need to do with the mapping works just fine.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 NH 中的一个错误。在 3.0 Alpha2 版本中已修复。
请参阅 NH-2261 和 NH-2149 供参考。
我在这里看到 2 个选项:
It is a bug in NH. It's fixed in the 3.0 Alpha2 version.
See NH-2261 and NH-2149 for reference.
I see 2 options here: