I'm struggling with a NHibernate related problem where I could use some input.
Introduction:
I have a legacy database where the relational concepts have not really been applied.
In the database I have an OrderLine
table which contains data for an order lines.
On top of that the table also contains all columns with Order
specific information. This could for example be order number of a customer.
E.x. If i have 10 order lines - then I have 10 rows in my OrderLines
table and each row has all the Order
specific data e.g. order number or customer information.
I did not want to have the above structure in my code so a view was created for Orders
so that I could map my Order
in NHibernate which then has a set/bag of OrderLines
which makes much more sense.
Mapping: (simplified)
<class name="Order" table="[view_Orders]">
<bag name="OrderLines">
</class>
<class name="OrderLine" table="OrderLines" />
The problem:
The complexity of the view makes it impossible to save to the view. When trying NHibernates throws this exception:
NHibernate.Exceptions.GenericADOException: could not insert: XXX ---> System.Data.SqlClient.SqlException: View or function 'view_Orders' is not updatable because the modification affects multiple base tables.
My NHibernate mapping is constructed as an Order
object which has a "set or bag" of OrderLine
objects. Ideally I would like NHibernate only to persist the set of OrderLine
objects instead of the whole object.
Is there a way of achieving this? I have tried locking the object using different lock modes but it did not help me.