Question

I am trying to convert my FluentNHibernate mappings to NHibernate Mapping By-code using NHibernate 3.3.3. The goal is to upgrade to NHibernate 3.3.3 and to cut down on the number of assemblies being distributed.

However when I compile and run I get the following exception:

NHibernate.MappingException: Multi-columns property can't be mapped through single-column API.

The XML mapping FluentNHibernate gets my looks like this:

<many-to-one cascade="none" class="TextDto" fetch="join" lazy="false" name="Name" not-found="ignore">
  <column name="NameTextId" unique="false" />
  <column name="LanguageId" unique="false" />
</many-to-one>

Here is my new By-Code mapping:

this.ManyToOne(u => u.Name, c =>
{
    c.Cascade(Cascade.None);
    c.Class(typeof(TextDto));
    c.Columns(
        x =>
        {
            x.Name("NameTextId");
            x.Unique(false);
        },
        x =>
        {
            x.Name("LanguageId");
            x.Unique(false);
        });
    c.Fetch(FetchKind.Join);
    c.Lazy(LazyRelation.NoLazy);
    c.NotFound(NotFoundMode.Ignore);
    c.Unique(false);
});

This is the old FluentNHibernate mapping:

References(x => x.Name)
    .Columns("NameTextId", "LanguageId")
    .Cascade.None()
    .Fetch.Join()
    .NotFound.Ignore()
    .Not.Unique()
    .Not.LazyLoad();

For Completeness the property type involved:

public class TextDto
{
    public TextCompositeId Id { get; set; }
    public string PluralText { get; set; }
    public string SingularText { get; set; }
    public override bool Equals(object obj)
    {
        var text = (TextDto)obj;
        if (text == null) return false;
        return this.Id.Equals(text.Id);
    }
    public override int GetHashCode()
    {
        return this.Id.GetHashCode();
    }
}

And an example of the property in an entity:

public class CharacteristicValue
{
    public CharacteristicValueCompositeId Id { get; set; }
    public TextDto Name { get; set; }
    public string LanguageIdentity { get; set; }
    public string Value
    {
        get
        {
            string value = null;
            if (this.ValueMultilingual != null) return this.ValueMultilingual.SingularText;
            else if (!string.IsNullOrEmpty(this.ValueMeta)) return this.ValueMeta;
            return value;
        }
    }
    public TextDto ValueMultilingual { get; set; }
    public string ValueMeta { get; set; }
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        if (object.ReferenceEquals(this, obj)) return true;
        CharacteristicValue characteristicValue = obj as CharacteristicValue;
        if (characteristicValue == null) return false;
        if (this.Id != characteristicValue.Id) return false;
        return true;
    }
    public override int GetHashCode()
    {
        return this.Id.GetHashCode();
    }
}

So, how do I get the xml-mapping I used to get with FluentNHibernate but with NHiberbate's Mapping By-Code?

Was it helpful?

Solution

In your mapping, remove the c.Unique(false); from the ManyToOne mapping. This setting we do apply for each column now.

this.ManyToOne(u => u.Name, c =>
{
    ... // the same as above

    // c.Unique(false); // it is setting now related to columns
});

And you will recieve

<many-to-one name="Name" class="TextDto" fetch="join" lazy="false" not-found="ignore">
  <column name="NameTextId" unique="true" />
  <column name="LanguageId" />
</many-to-one>

If you will change uniqueness on one of the columns:

x =>
{
    x.Name("NameTextId");
    x.Unique(true); // change here
},

The unique constraint would be added to that column:

<column name="NameTextId" unique="true" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top