Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

NaturalId and Map.Unique result in duplicate property mapping #141

Open
MichaelLogutov opened this Issue · 3 comments

3 participants

@MichaelLogutov

I want to map this simple class:

public class Country
{
    public virtual int Id { get; protected set; }
    public virtual string Code { get; protected set; }
    public virtual string Name { get; protected set; }
}

The "Code" property is actually unique so I thought to use it as natural id. But I wanted to specify the length of it. So I've created this map:

public class CountryMap : ClassMap<Country>
{
    public CountryMap ()
    {
        this.Id (x => x.Id);

        this.NaturalId ().Property (x => x.Code);
        this.Map (x => x.Code).Length (2).Unique ();

        this.Map (x => x.Name).Length (255);
    }
}

And I got this exception:
NHibernate.MappingException: Duplicate property mapping of Code found in Domain.Model.Geo.Country

The resulting HBM is this:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="false" default-cascade="all">
  <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true" dynamic-update="true" mutable="false" optimistic-lock="none" name="Domain.Model.Geo.Country, Parts.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Country`">
    <cache usage="read-only" />
    <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Id" />
      <generator class="identity" />
    </id>
    <natural-id>
      <property name="Code" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <column name="Code" />
      </property>
    </natural-id>
    <property name="Code" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Code" length="2" unique="true" />
    </property>
    <property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <column name="Name" length="255" />
    </property>
  </class>
</hibernate-mapping>

Packages I use:

<package id="FluentNHibernate" version="1.3.0.727" />
<package id="Iesi.Collections" version="3.2.0.4000" />
<package id="NHibernate" version="3.3.0.4000" />
<package id="NHibernate.Caches.SysCache" version="3.2.0.4000" />
@rohancragg

I have a similar problem. After having specifiec .UseOverridesFromAssemblyOf I am then using IAutoMappingOverride. When trying to specify that a string column is a NaturalId.ReadOnly I get the same error.

@rohancragg

It occurred to me to try using IgnoreProperty in the Override first...

This seems to prevent the error message (though the .ReadOnly() still seems not to be having the expected effect - CustomerCode column is still nullable)

public class CustomerConfigOverride: IAutoMappingOverride<Customer>
{
    public void Override(AutoMapping<Customer> mapping)
    {
        mapping.Schema("MySchema");
        mapping.IgnoreProperty(c => c.CustomerCode);
        mapping.NaturalId()
               .Property(c => c.CustomerCode)
               .ReadOnly();
...
@silversens

Same thing here, would be great if a ".Unique" was added to ".Property"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.