Object modified because of get

155 views
Skip to first unread message

Scott

unread,
Jan 21, 2011, 12:30:58 PM1/21/11
to nhusers
Using NHibernate 3
One of my objects has been getting marked as being modified which sets
off a chain reaction to update a bunch of related objects and I have
no idea why. In all the objects that get modified, the only thing that
changes is their version number so it makes me think its something to
do with the sets.

If I do just something simple like this:

Rosmat rosmat = Session.Get<Rosmat>(id);
ITransaction tx = Session.BeginTransaction();
tx.Commit();

Then the Rosmat object I got and a bunch of other related objects get
their version numbers updated.

Here's my mapping:

<class name="DatabaseModel.Models.Rosmat, DatabaseModel"
table="Rosmat" >
<cache usage="nonstrict-read-write" region="RosmatCache" />
<id name="RefId" type="guid">
<generator
class="DatabaseModel.Services.DataIntegrity.GuidExistingGenerator,
DatabaseModel" />
</id>
<version name="Version" type="Int32" generated="never">
<column name="Version" not-null="true" />
</version>
<property name="Title" column="Title" type="string" not-null="true"/>
<property name="AbbrevTitle" column="AbbrevTitle" type="string" not-
null="false"/>
<property name="Expiration" column="Expiration" type="date" not-
null="false"/>
<property name="ImportCode" column="ImportCode" type="string" not-
null="false"/>

<property name="MarkingRule" column="MarkingRule" type="int" not-
null="true"/>

<property name="JsonStorageString" type="string" >
<column name="JsonStorage" sql-type="nvarchar(max)" not-
null="false" />
</property>

<many-to-one name="Owner" column="OwnerUserInfoRefId"
class="DatabaseModel.Models.UserInfo"/>
<many-to-one name="School" column="SchoolInfoRefId"
class="DatabaseModel.Models.SchoolInfo"/>

<set name="StudentLists" order-by="title" lazy="true">
<cache usage="nonstrict-read-write" region="RosmatCache" />
<key column="RosmatRefId" />
<one-to-many class="DatabaseModel.Models.StudentList" />
</set>

<set name="RosmatPermissions" lazy="true">
<key column="RosmatRefId" />
<one-to-many class="DatabaseModel.Models.RosmatPermission" />
</set>

<set name="Specialties" table="RosmatSpecialty" lazy="true">
<key column="RosmatRefId" />
<many-to-many column="SpecialtyRefId"
class="DatabaseModel.Models.Specialty" />
</set>

<set name="SourceRosmats" table="SourceRosmat" lazy="true">
<key column="RosmatRefId" />
<many-to-many column="ParentRosmatRefId"
class="DatabaseModel.Models.Rosmat" />
</set>
</class>

José F. Romaniello

unread,
Jan 21, 2011, 12:40:08 PM1/21/11
to nhu...@googlegroups.com
it must be a ghost:

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


José F. Romaniello

unread,
Jan 21, 2011, 12:41:33 PM1/21/11
to nhu...@googlegroups.com
I always send those two links and my head sings

Scott

unread,
Jan 21, 2011, 1:08:47 PM1/21/11
to nhusers
Ahh, that helps a lot but one thing the blog post doesn't cover is how
to fix them.
One of my problems I noticed right away is that I am mapping an enum
to an int what should I be doing instead?




On Jan 21, 11:41 am, José F. Romaniello <jfromanie...@gmail.com>
wrote:
> I always send those two links and my head singshttp://www.youtube.com/watch?v=iCHFVTQKqdQ
>
> 2011/1/21 José F. Romaniello <jfromanie...@gmail.com>
>
>
>
> > it must be a ghost:
>
> >http://fabiomaulo.blogspot.com/2008/10/how-test-your-mappings-ghostbu...
>
> > <http://fabiomaulo.blogspot.com/2008/10/how-test-your-mappings-ghostbu...>
> >http://jfromaniello.blogspot.com/2010/02/nhibernate-ghostbuster-versi...
>
> > 2011/1/21 Scott <scott.r.down...@gmail.com>
> >> nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >
> >> .

José F. Romaniello

unread,
Jan 21, 2011, 1:25:32 PM1/21/11
to nhu...@googlegroups.com
Maybe you have a nullable column, with nullable values mapped to a not nullable property in .net:
 imagine you have an int column, with null values, mapped to a System.Int32:
 in this case nhibernate will load the value as 0... and when checking if the value has changed... 0 != null. .so it will update the value to 0.

Another case is when you have wrong types suchs a wrong mapped enum.
The ghostbuster will tell you what is the column/property with problems.

2011/1/21 Scott <scott.r...@gmail.com>
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

Scott

unread,
Jan 21, 2011, 2:47:17 PM1/21/11
to nhusers
So doing the test, its definitely because I'm mapping an int to an
enum.

Now what should I do instead? Have an NHibernate mapped property that
is int to int and have another property or method that I actually use
to get and set the NHibernate property and do the enum to int type
setting?




On Jan 21, 12:25 pm, José F. Romaniello <jfromanie...@gmail.com>
wrote:
> Maybe you have a nullable column, with nullable values mapped to a not
> nullable property in .net:
>  imagine you have an int column, with null values, mapped to a System.Int32:
>  in this case nhibernate will load the value as 0... and when checking if
> the value has changed... 0 != null. .so it will update the value to 0.
>
> Another case is when you have wrong types suchs a wrong mapped enum.
> The ghostbuster will tell you what is the column/property with problems.
>
> 2011/1/21 Scott <scott.r.down...@gmail.com>
> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>>

Fabio Maulo

unread,
Jan 21, 2011, 2:57:58 PM1/21/11
to nhu...@googlegroups.com
You don't have to be so clear in the mapping.
If you don't specify the attribute 'type' in the mapping, I'm sure that NH will do the right thing.
In practice the rule of thumb, of the mapping, is: 
Don't try to be so "clear" because it will be more "dark"

To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Scott

unread,
Jan 21, 2011, 3:38:52 PM1/21/11
to nhusers
Huh, that works. I just always assumed the more explicate I was the
better but this proves me wrong.
> > > > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>
> Fabio Maulo
Reply all
Reply to author
Forward
0 new messages