开发者

using NHibernate on a table without a primary key

开发者 https://www.devze.com 2022-12-15 20:30 出处:网络
I am (hopefully) just about to start on my first NHibernate project and have come across a stumbling block. Would appreciate any advice.

I am (hopefully) just about to start on my first NHibernate project and have come across a stumbling block. Would appreciate any advice.

I am working on a project where my new c开发者_StackOverflow社区ode will run side-by-side with legacy code and I cannot change the data model at all. I have come across a situation where one of the main tables has no primary key. It is something like this:

Say there is an order table and a product table, and a line_item table which lists the products in each order (i.e. consits of order_id, product_id, and quantity). In this data model it is quite possible to have 2 line items for the same product in the same order. What happens in the existing code is that whenever the user updates a line item, all line items for that order are deleted and re-inserted. Since even a compound key of all the fields in the line_item table would not necessarily be unique, that is the only possible way to update a line item in this data model.

I am prepared to guarantee that I will never attempt to update or delete an indivdual line item. Can I make my NHibernate code work in the same way as the existing code? If not, does this mean I (a) I cannot use NHibernate at all; (b) I cannot use NHibernate to map the line_item table; or (c) I can still map this table but not its relationships

Thanks in advance for any advice


I think if you map it as a bag collection on the Order (with inverse="false") it would work.

Collection Mapping

Note: Large NHibernate bags mapped with inverse="false" are inefficient and should be avoided; NHibernate can't create, delete or update rows individually, because there is no key that may be used to identify an individual row.

They warn against it but it sounds like what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消