-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Specify insert="false" on single column in many-to
PostPosted: Fri Aug 01, 2008 10:36 am 
Newbie

Joined: Tue Jul 29, 2008 5:11 am
Posts: 5
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.