1

I'm referring to this article: http://ayende.com/blog/3960/nhibernate-mapping-one-to-one

<class name="UniDirectional.UConversation, NHibernateOneToMany" table="conversation">
  <id name="Id">
    <generator class="native" />
  </id>
  <property name="Subject"/>
  <list name="MessageList" cascade="all">
    <key column="ConversationId"/>
    <index column="`Order`"/>
    <one-to-many class="UniDirectional.UMessage, NHibernateOneToMany"/>
  </list>
</class>

<class name="UniDirectional.UMessage, NHibernateOneToMany" table="message">
  <id name="Id">
    <generator class="native" />
  </id>
  <property name="Text"/>
  <property name="ConversationId" type="Int32" column="ConversationId" />
</class>

Now the C# Code:

session = factory.OpenSession();
transaction = session.BeginTransaction();

UniDirectional.UMessage message1 = new UniDirectional.UMessage();
message1.Text = "Text1";
niDirectional.UMessage message2 = new UniDirectional.UMessage();
message2.Text = "Text2";
ArrayList messageList = new ArrayList();
messageList.Add(message1);
messageList.Add(message2);

UniDirectional.UConversation someConversation = new UniDirectional.UConversation();
someConversation.Subject = "a test conversation";
someConversation.MessageList = (IList)messageList;

session.Save(someConversation);
transaction.Commit();

Assert.AreEqual(message1.ConversationId, someConversation.Id); // FAIL

The message1.ConversationId is always 0, it makes no difference if I try to reload the Messages. But if I restart the whole program and I load the existing Messages the ConversationId is filled correctly. Why NHibernate doesn't update the ConversationId?

1 Answer 1

0

The point is, that NHibernate is not managing the C# runtime objects.

Unless we explictily set the relation both ways:

foreach(message in someConversation.MessageList)
{
    message.ConversationId = someConversation.Id; 
}

the message.ConversationId won't be assigned at runtime. It is not what NHibernate doing

Also, if the someConversation ID is generated by identity (native), this could not work properly, because before Save() is called the someConversation.Id could be 0

The best what we can do is to map the parent not as int but as UConversation

<class name="UniDirectional.UMessage, NHibernateOneToMany" table="message">
  <id name="Id">
    <generator class="native" />
  </id>
  <property name="Text"/>
  <property name="ConversationId" type="Int32" column="ConversationId" 
    insert="false" update="false />
  <many-to-one name="Conversation" column="ConversationId" />
</class>

Here, we extended the C# entity UMessage with a new property Conversation

public virtual UConversation Conversation { get; set; }

And this one will be persisted, The ConversationId (mapped to same column) will be there still, e.g. for querying, order by... but won't be INSERTed or UPDATEd

And that will help us to correctly make the mapping in the runtime

foreach(message in someConversation.MessageList)
{
    message.Conversation = someConversation; // assing parent
}

But in that case, the message.ConversationId won't be again automatically populated, unless we will reload it by NHibernate.

And finally, because we do have both ends (entities) now mapped, we can improve the mapping with the inverse attribute. This will improve over all performance during WRITE operations, but require that both ends are correctly set in runtime (child to parent and parent to child)

<list name="MessageList" cascade="all" inverse="true">
4
  • this works of course, thanks! It's for me a little bit odd, because the ConversationId is correctly set in the DB, but Hibernate doesn't update its own references ... Jan 28, 2014 at 13:51
  • NHibernate does! really ;) but only during the load. What is set on the application side... is fully in our hands. So you can try like: session.Clear() and then session.Get<UConversation >(xxx) where xxx is the id just generated. Then you will see, that the loaded object is fully populated Jan 28, 2014 at 13:53
  • you right. session.Refresh(message1); also seems to work and I think this should be faster. Jan 28, 2014 at 14:02
  • The point is, NHibernate, will do all the setting, specified in the mapping. On the other hand, when we do pass object to the NHiberante, we are responsible to properly set all properties. Mostly the writable ;) Jan 28, 2014 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.