我可以将 nHibernate 与没有引用完整性的旧数据库一起使用吗?
如果我有一个没有引用完整性或键的旧数据库,并且它使用存储过程进行所有外部访问,那么使用 nHibernate 来持久化实体(对象图)有什么意义吗?
另外,SP 不仅包含 CRUD 操作,还包含业务逻辑...
我开始认为坚持使用自定义 ado.net DAL 会更容易:(
干杯
Ollie
If I have a legacy database with no referential-integrity or keys and it uses stored procedures for all external access is there any point in using nHibernate to persist entities (object-graphs)?
Plus, the SP's not only contain CRUD operations but business logic as well...
I'm starting to think sticking with a custom ado.net DAL would be easier :(
Cheers
Ollie
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您很可能可以。但你可能不应该:-)
Hibernate 本身并不关心引用完整性;虽然显然需要在关联表之间有某种链接,但实际的 FK 约束是否存在并不重要。例如,如果
Product
被映射为多对一
到Vendor
,PRODUCTS
表应该有某种排序其中的VENDOR_ID
,但不一定是 FK。根据您的 SP 签名,您可能能够也可能无法在映射中将它们用作自定义 CRUD;如果 SP 中确实具有在所有 CRUD 操作期间应用的业务逻辑,那么这可能是您的第一个潜在问题。
最后,如果您的 SP 确实用于所有 CRUD 操作(包括所有可能的查询),那么可能不值得尝试将 Hibernate 引入其中 - 您将几乎一无所获,而且您'还有另一层需要处理。
You most likely CAN. But you probably shouldn't :-)
Hibernate does not care about referential integrity per se; while it obviously needs to have some sort of link between associated tables, it does not matter whether actual FK constraint exists. For example, if
Product
is mapped asmany-to-one
toVendor
,PRODUCTS
table should have some sort ofVENDOR_ID
in it but it doesn't have to be a FK.Depending on your SP signatures, you may or may not be able to use them as custom CRUD in your mappings; if SPs indeed have business logic in them that is applied during all CRUD operations, that may be your first potential problem.
Finally, if your SPs are indeed used for ALL CRUD operations (including all possible queries) it's probably just not worth it to try and introduce Hibernate to the mix - you'll gain pretty much nothing and you'll have a yet another layer to deal with.
好吧,问题的一个例子是这样的:
一个 SP 使用类似于下面的 sql 语句来选择下一个要插入到表的“Id”列中的 Id(该列只是一个 int 列,而不是一个标识列) ),
语句:“select @cus_id = max(id) + 1 fromcustomers”,
因此一旦计算出下一个 id,它就会与其他数据一起插入到表 A 中,然后将一行插入到表 B 中,其中有对表 A 的引用(没有外键约束)表 A 中的另一列,然后最后使用与表 A 相同的引用将一行插入到表 C 中。
当我使用流畅的 NH 将其映射到 NH 时,映射生成了正确的“插入”sql 语句对于第一个表,但是当第二个表映射为“参考”时,生成了“更新”sql 语句,我期望看到“插入”语句...
现在事实是没有标识列,没有键,并且没有参照完整性对我来说意味着我不能保证关系是一对一、一对多等...
如果这是真的,NH(流畅)如何配置...
欢呼
Ollie
okay, an example of the problem is this:
A SP uses a sql statement similar to the following to select the next Id to be inserted into the 'Id' column of a table (this column is just an int column but NOT an identity column),
statement: 'select @cus_id = max(id) + 1 from customers',
so once the next id is calculated it's inserted into table A with other data, then a row is inserted into table B where there is ref to table A (no foreign key constraint) on another column from table A, then finally a row is inserted into table C using the same ref to table A.
When I mapped this into NH using the fluent NH the map generated a correct 'insert' sql statement for the first table but when the second table was mapped as a 'Reference' an 'update' sql statement was generated, I was expecting to see an 'insert' statement...
Now the fact there is no identity columns, no keys and no referential-integrity means to me that I can't guarantee relationship are one-to-one, one-to-many etc...
If this is true, how can NH (fluent) configured either...
Cheers
Ollie