将 NUMERIC(5,0) 映射到字符串时保留前导零

发布于 2024-10-27 05:26:52 字数 250 浏览 1 评论 0原文

希望这是一个简单的问题。我在 AS/400 上有一个旧版 DB2 数据库,其中邮政编码等字段存储为 NUMERIC。当在 NHibernate 中映射它们时,它们被视为整数并且前导零会丢失。通常,我会使用

SELECT DIGITS(field) FROM TABLE

来保留前导零,但我的理解是,如果我创建公式来纠正格式,我会强制该字段变为只读。

将 NUMERIC 类型(及其前导数字)映射到字符串类型并再次映射回来的正确方法是什么?

Hopefully this is an easy question. I have a legacy DB2 database on an AS/400 where fields like Zip Code are stored as NUMERIC. When mapping them in NHibernate they are treated as integers and the leading zeros are lost. Typically I would use

SELECT DIGITS(field) FROM TABLE

to preserve leading zeros but its my understanding that if I go creating formulas to correct formatting I force the field to become read only.

What is the correct way to map the NUMERIC type (with its leading digits) to a string type and back again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作妖 2024-11-03 05:26:52

你试图在错误的层面上处理它。

前导零是一个显示问题;只需使用 "00000" 作为格式字符串。

You are trying to deal with it at the wrong level.

Leading zeroes are a display concern; just use "00000" as your format string.

风渺 2024-11-03 05:26:52

Diego 是对的,但如果您确实需要将邮政编码表示为域中的字符串,您可以实现自己的 IUserType 并在映射中使用它。但是,我不确定您是否能够处理将前导零放入数字数据库列(如果这是您需要的)。我认为如果您将邮政编码存储为数据库中的“392”,然后将其显示为“00392”,那就很容易了。

要快速概述创建 IUserType 所涉及的内容,请查看此博客文章:使用 NHibernate 的 IUserType 将字符串映射到布尔值

Diego is right, but if you really need to represent the zip codes as strings in your domain you could implement your own IUserType and use it in your mappings. However, I'm not sure that you'll be able to handle getting the leading zeroes into the numeric database column (if this is something you need). I think it would be easy enough if you stored your zip code as '392' in the database and then presented it as '00392' though.

For a quick overiew of what's involved in creating an IUserType take a look at this blog post: Mapping Strings to Booleans using NHibernate's IUserType

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文