NHibernate、遗留数据库、不存在的外键
我正在从事的项目有一个遗留数据库,其中包含大量用于更改应用程序行为的信息。基本上,我被一些东西困住了,我必须超级小心地改变。
说到我的问题。在这个数据库中有一个表,在这个表中有一个列。该列包含整数,并且该列的大多数预先存在的数据的值为零。
问题在于,该列实际上是对另一个实体的外键引用,只是从未在数据库模式中如此定义。
现在,在我的新代码中,我定义了 Fluent-NHibernate 映射,将此列视为引用,这样我就不必直接在代码中处理实体 id。在我遇到此列中值为 0 的实体之前,此方法工作正常。
NHibernate 认为 0 值是有效的引用。当我的代码尝试使用该引用的对象时,我得到一个 ObjectNotFoundException ,因为显然我的数据库中没有 id 为 0 的对象。
我怎样才能通过映射或某种约定(我正在使用 Fluent-nhibernate),让 NHibernate 将 0 的 id 视为 NULL?
The project I'm working on has a legacy database with lots of information in it that's used to alter application behavior. Basically I'm stuck with something that I have to be super careful about changing.
Onto my problem. In this database is a table and in this table is a column. This column contains integers and most of the pre-existing data have a value of zero for this column.
The problem is that this column is in fact a foreign key reference to another entity, it was just never defined as such in the database schema.
Now in my new code I defined my Fluent-NHibernate mapping to treat this column as a Reference so that I don't have to deal with entity id's directly in my code. This works fine until I come across an entity that has a value of 0 in this column.
NHibernate thinks that a value of 0 is a valid reference. When my code tries to use that referenced object I get an ObjectNotFoundException as obviously there is no object in my database with an id of 0.
How can I, either through mapping or some kind of convention (I'm using Fluent-nhibernate), get NHibernate to treat id's that are 0 the same as if it was NULL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现 API 告诉 NHibernate 忽略未找到的引用 (NotFound.Ignore()),而不是抛出异常。我对网上发现的所有提到的 SetAttribute() 感到困惑,它适用于比我正在使用的旧版本的 Fluent-nhibernate。
I found the API to tell NHibernate to ignore references that aren't found (NotFound.Ignore()), rather than throw the exception. I was confused by all the mentions of SetAttribute() I found online which is for an older version of fluent-nhibernate than I am using.
我也有同样的情况。 not-found=ignore 的问题是,每次您尝试访问该关系时,它都会重新查询该关系,即使它已经在原始查询中运行过。基本上,hibernate 不存储关系另一方没有记录的事实。您可以在调试日志中看到这一操作。这是我当前项目的一个示例。
您可以看到它尝试绑定“”,该应用程序的数据库使用空白空间来表示 null(我知道这很愚蠢)。但是每次nhibernate遇到这种情况时,它都会尝试从DB中查找它,因为它在缓存中找不到该记录并且不知道它实际上是NULL。
如果我们可以在配置中指定要忽略的默认空值,那就太好了。目前解决这个问题的唯一方法是使用查询来加载关系,而不是依赖 nhibernate 生成的查询。
I'm in the same situation. The problem with not-found=ignore is that it will requery the relationship everytime you try to access it even though it has already been run in the original query. Basically, hibernate doesn't store the fact that there is no record for the otherside of the relationship. You can see this in action in the debug log. Here's an example from my current project.
You can see it trying to bind ' ', the DB for this app uses empty space to represent null (stupid I know). But each time nhibernate encounters this, it will try to look it up from the DB because it can't find the record in the cache and doesn't know that it's actually NULL.
It would be nice if we could specify a default null value to ignore in configuration. Currently the only way to get around this is to use queries to load your relationships rather than relying on nhibernate generated queries.