views:

254

answers:

1

Hello colleagues. I've got a question. I use NHibernate with MySql. At my entities I use Id(PK) for my business-logic usage and Guid(for replication). So my BaseDomain:

 public class BaseDomain
 {
    public virtual int Id { get; set; }
    public virtual Guid Guid { get; set; }
    public class Properties
    {
        public const string Id = "Id";
        public const string Guid = "Guid";
    }
    public BaseDomain() { }
 }

My usage domain:

public class ActivityCategory : BaseDomain
{
    public ActivityCategory() { }
    public virtual string Name { get; set; }
    public new class Properties
    {
        public const string Id = "Id";
        public const string Guid = "Guid";
        public const string Name = "Name";
        private Properties() { }
    }
}

Mapping:

 <class name="ActivityCategory, Clients.Core" table='Activity_category'>
   <id name="Id" unsaved-value="0" type="int">
      <column name="Id" not-null="true"/>
      <generator class="native"/>
   </id>
   <property name="Guid"/>
   <property name="Name"/>
 </class>

But when I insert my entity:

[Test]
    public void Test()
    {
        ActivityCategory ac = new ActivityCategory();
        ac.Name = "Test";
        using (var repo = new Repository<ActivityCategory>())
            repo.Save(ac);
    }

I always get '00000000-0000-0000-0000-000000000000' at my Guid field. What should I do for generate right Guid. May be mapping?

Thanks a lot!

+1  A: 

From a NHibernate perspective, you should either set the guid in C# or tell NHibernate that it is generated by the database.

In the former case, set the guid property in the constructor.

public class BaseDomain
{
    public BaseDomain()
    {
        Guid = Guid.NewGuid();
    }
}

You map a property whose value is generated in the database like this. Depending on how the value is generated, you may also need to exclude the property from insert statements.

<class name="ActivityCategory, Clients.Core" table="Activity_category">
    <id name="Id" not-null="true" >
        <generator class="native" />
    </id>
    <property name="Guid" generated="insert" insert="false" update="false" />
    <property name="Name" />
</class>
Lachlan Roche
Big thanks for answer. I want generate guid by db. A try your variant generated="insert", but get'00000000-0000-0000-0000-000000000000' again. What do i do wrong?
Andrew Kalashnikov
You probably need that column to be null or absent from the insert statement. I have updated the answer to exclude it from insert.
Lachlan Roche
After this changes I've got:NHibernate: INSERT INTO clients.Activity_category (Name) VALUES (?p0);?p0 = 'Test'NHibernate: SELECT LAST_INSERT_ID()NHibernate: SELECT activityca_.Guid as Guid0_ FROM clients.Activity_category activityca_ WHERE activityca_.Id=?p0;?p0 = 4NHibernate: UPDATE clients.Activity_category SET Guid = ?p0, Name = ?p1 WHERE Id = ?p2;?p0 = 00000000-0000-0000-0000-000000000000, ?p1 = 'Test', ?p2 = 4
Andrew Kalashnikov
That looks good until the update, you can see the SELECT for the db generated Guid. You should not have to specify update="false" but I have added that as well.
Lachlan Roche
Thanks a lot for your answers. Now I haven't 00000000-0000-0000-0000-000000000000 at Guid field, but have null value. What should I do to make MYSql generate guid to me?
Andrew Kalashnikov
May be mysql hasn't special type for guid and can't generate it hrough newuid()? So I can only generate it from my client?
Andrew Kalashnikov
The mysql function is UUID() http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-functions.html#function_uuid
Lachlan Roche
Sorry it's just writing mistake:(Can I generate guid by db with NHibernate without sp?
Andrew Kalashnikov