Re: [nhusers] Entity version not updated when member set item modified

27 views
Skip to first unread message

Gunnar Liljas

unread,
Sep 24, 2015, 5:15:42 PM9/24/15
to nhu...@googlegroups.com
With RemoveProperty commented out, nothing is done with fromPerson, hence no version increment.

I'm guessing the cascade "deletes" the property.

Maybe instead of transferring you could remove the property from fromPerson and add a cloned property to toPerson.

 

2015-09-24 20:37 GMT+02:00 Peter V <pvhe...@gmail.com>:
I have the following NHibernate mapping:

  <class name="Person" proxy="Person" table="Person_">

    <id name="OID" column="OID_" type="Guid">
      <generator class="guid.comb"/>
    </id>

    <version name="Version" column="Version_"/>

    <set name="Properties" lazy="true" inverse="true" cascade="all-delete-orphan">
      <key column="PersonOID_"/>
      <one-to-many class="Property"/>
    </set>

  </class>


And the following C# code:

public class Property
{
    public Person Owner;
}

public class Person
{
    private ISet _properties;

    public void AddProperty(Property property)
    {
        property.Owner = this;
        _properties.Add(property);
    }

    public void RemoveProperty(Property property)
    {
        property.Owner = null;
        _properties.Remove(property);
    }
}

...

public void TransferProperty(Property p, Person fromPerson, Person toPerson)
{
    //fromPerson.RemoveProperty(p);
    toPerson.AddProperty(p);
}


A call to TransferProperty results in the entity version for toPerson getting incremented, but no change to the entity version for fromPerson.

This is causing me a problem when multiple users call TransferProperty at the same time -- because fromPerson version doesn't change, there is no PersistenceException thrown when two users try to transfer property from the same person at the same time.

Any thoughts on the best way to modify this code so that calling TransferProperty will increment the entity versions for both toPerson and fromPerson???

Thanks in advance!
Peter

P.S. I commented out the RemoveProperty call in TransferProperty, because it was resulting in "ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)".

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+u...@googlegroups.com.
To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

Peter V

unread,
Sep 24, 2015, 8:11:28 PM9/24/15
to nhusers
Thanks, that's definitely a solution.
But I'd prefer not to have to clone it if there's another way -- my actual Property class is a lot more complex than what I've shown here, and cloning it may introduce some other issues.

The thing is, even with RemoveProperty commented out, the property is getting removed from fromPerson.
So I don't understand why the version of fromPerson isn't incremented.

Peter

Gunnar Liljas

unread,
Sep 25, 2015, 1:35:40 AM9/25/15
to nhu...@googlegroups.com
It is removed from toPerson as a side effect since the collection is mapped as inverse, and you change the Owner. But NHibernate has no way of knowing this.

Since Property really seems to be a full blown entity, maybe you can handle deletions of it explicity (with session.Delete), and thereby be able to set the cascade to not delete orphans.

Peter V

unread,
Sep 25, 2015, 10:50:07 AM9/25/15
to nhusers
But is there an elegant way to just force NHibernate to update the version number of both toPerson and fromPerson when TransferProperty is called?

Locking fromPerson as dirty doesn't do it (I guess because nothing actually changed), and I'm trying to avoid modifying the class mapping (the regression test effort of doing so would be huge because there is so much other code that references it).

Thanks,
Peter

Gunnar Liljas

unread,
Sep 25, 2015, 11:37:26 AM9/25/15
to nhu...@googlegroups.com
Do you have another property on Person that can be changed explicitly instead, perhaps a ModifiedOn timestamp, thereby triggering version increment?

Peter Vanderheyden

unread,
Sep 25, 2015, 12:26:04 PM9/25/15
to nhu...@googlegroups.com
Unfortunately no.  Just OID and Version.




--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/-Q8Q7_8jyI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+u...@googlegroups.com.

Peter V

unread,
Sep 25, 2015, 2:32:57 PM9/25/15
to nhusers
I'm thinking the best/only way to go is to use something like "LockMode.PESSIMISTIC_FORCE_INCREMENT".

Peter





On Friday, September 25, 2015 at 12:26:04 PM UTC-4, Peter V wrote:
Unfortunately no.  Just OID and Version.



On 25 September 2015 at 11:37, Gunnar Liljas wrote:
Do you have another property on Person that can be changed explicitly instead, perhaps a ModifiedOn timestamp, thereby triggering version increment?
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+unsubscribe@googlegroups.com.

To post to this group, send email to nhu...@googlegroups.com.
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "nhusers" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nhusers/-Q8Q7_8jyI4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nhusers+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages