views:

102

answers:

1

Hi all

Just a quickie ... I have the following ID generator strategy for one of my mapped classes:

<id name="UID" type="System.Guid">
    <column name ="UID" sql-type ="uniqueidentifier" />
    <generator class="guid.comb" />
</id>

The entity in question is involved in synchronisation / merging behaviours from which it is necessary to have a globally unique identifier.

When an entity is created on a client application for the first time, it's UID property gets assigned so that it is the same value of the equivilent entity on the server.

However the above ID generator strategy overwrites any value provided for new/transient entities.

Whats the fix? Will I have to remove the generator strategy and assign my own GUIDs? Or is the generator strategy configurable to only generate a guid.comb when required?

A: 

I think you can accomplish this by making UID a private field and controlling access through the property.

public class MyClass
{
    private Guid _uid;

    protected MyClass() { // parameterless ctor for NH }

    public MyClass(Guid uid) { _uid = uid; // assign on creation }

    public Guid Uid
    {
        get { return _uid; }
        private set { // do nothing or compare _uid to Guid.Empty and set }
    }
} 
Jamie Ide
ok thanks, I'll try this and get back to you :)
Rabid
Unfortunately NHibernate appears to be updating it's internal entity state with a generated UID, resulting in the following exception:identifier of an instance of csl.Risk.Structure.RiskClass was altered from 12ae645e-c50f-4fc5-b0ff-9db900bd9a4e to 42e565c7-8dd3-416f-89a2-9db801086d93; where the 1st guid is a generated one and the second is the assigned one. Bummer :(
Rabid
New answer: You will have to remove the generator strategy and assign your own GUIDs. :-)
Jamie Ide
I am going down this road, `NHibernate.Id.GuidCombGenerator.Generate(null, null);` does produce a comb guid btw so am happy ... Let's hope it doesn't confuse the transient detection behaviour! I'll score you once I've verified it.
Rabid
thanks ... works nicely.
Rabid