-->
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.  [ 5 posts ] 
Author Message
 Post subject: Component Mapping And SaveOrUpdateCopy()
PostPosted: Tue May 13, 2008 10:45 am 
Newbie

Joined: Tue May 13, 2008 10:22 am
Posts: 3
Hi, guys....I got into an issue and I'm trying to solve it for a time with no success.....hope you can give me a piece of advice here. I'll explain the situation using a simple test :

Supose we have two classes like this:

Code:
public class Address
   {
      public string City { get; set; }
      public string Street { get; set; }
      public int Block { get; set; }      
      
   }


and
Code:
public class Person
   {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public Address Address{get; set; }      
   }

and Address property of Person is mapped as component:
Code:
<class name="Person">
    <id
        name="Id"   type="guid">
      <generator class="guid" />
    </id>
    <property name="Name" type="string" />
   <component name="Address" class="Address">
     <property name="City" type="string" />
     <property name="Street" type="string" />
     <property name="Block" type="int" />    
   </component>
  </class>

then the following test will fail :
Code:
[Test]
public void Should_Save_Person_Without_Address()
{
   var person = new Person { Name = "Bill" };
      
   using (var session = sessionFactory.OpenSession())
   using (var transaction = session.BeginTransaction())
   {
      session.SaveOrUpdateCopy(person);
      transaction.Commit();
   }
}

I tend to believe this is a bug...as SaveOrUpdate() works....I need to use SaveOrUpdateCopy() to avoid exceptions when using different sessions
I'm using NHibernate 2.0.0.1001


Top
 Profile  
 
 Post subject: Component Mapping And SaveOrUpdateCopy()
PostPosted: Tue May 13, 2008 12:24 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

Can you post the exception you're getting?
(Is it a NullReferenceException because you haven't initialised the Address property of your Person object?)

It seems to work if you try:
Code:
var person = new Person { Name = "Bill", Address = new Address() };


The documentation states:
Quote:
... The null value semantics of a component are ad hoc. ...

http://www.hibernate.org/hib_docs/nhibernate/html/components.html

This does seem a little strange, so it's not clear whether this is by design or not.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 5:25 am 
Newbie

Joined: Tue May 13, 2008 10:22 am
Posts: 3
Thanks Richard for your attention .....yes you are right ...its a NullRefferenceException .....how can I override this situation as I have situations when the component has to be null .....the documentation states :
Quote:
The null value semantics of a component are ad hoc. When reloading the containing object, NHibernate will assume that if all component columns are null, then the entire component is null. This should be okay for most purposes.

I understand this as - the component instance is built based on collumn values that represent it and if all null component will be null....but how about viceversa?....shouldn't it be true? if component is null all collumns that represent it should be populated with null??....as I wrote in first post, when using SaveOrUpdate it works fine(but I have other problems in this case because of our architecture)....When using SaveOrUpdateCopy() it throws(and I need it to work to avoid problems with SaveOrUpdate).


Top
 Profile  
 
 Post subject: Component Mapping And SaveOrUpdateCopy()
PostPosted: Thu May 15, 2008 5:42 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

Quote:
....but how about viceversa?....shouldn't it be true?


Unfortunately the documentation doesn't state if the reverse is true, but that would seem like reasonable assumption. You could consider raising a JIRA if you think it's a bug.

Quote:
... as I have situations when the component has to be null.


Unfortunately I don't know of another workaround than the one I suggested. Sorry.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 7:04 am 
Newbie

Joined: Tue May 13, 2008 10:22 am
Posts: 3
Thanks again, Richard...I appreciate it very much...I'll keep looking for a solution :-)


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

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.