Hibernate version: 1.2.1
I'm a beginner at NH and I find a lot of cases where I want to do what is requested here:
http://jira.nhibernate.org/browse/NH-1148
But since it is not possible (yet) I think there must be a workaround?
For example there are a lot of cases in our (legacy) database like:
TableName -> PrimaryKey
OrderLine -> OrderNo, LineNo
Component -> OrderNo, ComponentNo
The Component table have many-to-one a relationship with OrderLine. I currently map like this:
Code:
<class name="OrderLine" table="OrderLine">
<composite-id>
<key-property name="OrderNo" />
<key-property name="LineNo" />
</composite-id>
<bag name="Components" inverse="true" cascade="none">
<key>
<column name="OrderNo" />
<column name="BelongsToOrderLineNo" />
</key>
<one-to-many class="Component"/>
</bag>
</class>
<class name="Component" table="Component">
<composite-id>
<key-property name="OrderNo" />
<key-property name="ComponentNo" />
</composite-id>
<property name="OrderLineNo" />
<many-to-one name="BelongsToOrderLineNo"
class="OrderLine"
cascade="none"
insert="false"
update="false">
<column name="OrderNo" />
<column name="OrderLineNo" />
</many-to-one>
</class>
The problem is that since I cannot put
Code:
<column name="OrderNo" insert="false" update="false" />
or
Code:
<key-property name="OrderNo" insert="false" update="false" />
I have to disable the whole many-to-one relationship for insert/update and manage both the Component.OrderLineNo property and the Component.BelongsToOrderLineNo property myself to keep them in sync. I currently do this in the setter for the Component.BelongsToOrderLineNo property:
Code:
Public Overridable Property BelongsToOrderLineNo() As OrderLine
Get
Return _orderLine
End Get
Set(ByVal value As OrderLine)
If value.OrderNo <> Me.OrderNo Then
Throw New InvalidOperationException("BelongsToOrderLineNo cannot have different OrderNo.")
End If
Me.OrderLineNo = value.OrderLineNo
_orderLine = value
End Set
End Property
Now say that a Component object has a reference to a OrderLine object in its BelongsToOrderLineNo property. If the OrderLineNo property on that OrderLine object changes I need to sync this too. This becomes problematic and the problem is that there are two places where the relationship is defined. One in the Component.BelongsToOrderLineNo property and one in the Component.BelongsToOrderLineNo.OrderLineNo property.
Ok I know primary key values should not change, I should use a surragate key instead of composite-id and so on but this is not just possible since the database is used by a myriad of other tools and users that are used to the current design and a lot of other issues.
Now the request in the link above would solve this problem by letting me eliminate the Component.OrderLineNo property and have NH manage the relationship minus the OrderNo column. Is there some way to emulate this since it is not implemented yet? My current understanding of NH is not very advanced so maybe I am missing something basic?