4

I have an pretty big database and I have to map two entities using NHibernate 3.3.1 byCode mapping (please take in account that I'm not using FluentNHibernate, but built-in mappings)

Entities are in a many to many relationship through and unique key, not primary key.

Here are my examples

    public class Achievable : Entity
    {
        public Achievable()
        {
            Uniqueidentifier = Guid.NewGuid();
        }

        public virtual string Name { get; set; }

        public virtual Guid Uniqueidentifier { get; set; }

        public virtual IList<Kitting> Kittings { get; set; }
    }

    public class Kitting : Entity
    {
        public virtual string Name { get; set; }

        public virtual IList<Achievable> Achievables { get; set; }
    }

So I have to link Achievable table to Kitting using third table AchievableKittings through Uniqueidentifier property

I've written the following mapping classes :

public class AchievableMap : ClassMapping<Achievable>
{
    public AchievableMap()
    {
        Property(x => x.Name);
        Property(x => x.Uniqueidentifier, m =>
                                              {
                                                  m.Unique(true);
                                                  m.NotNullable(true);
                                              });

        Bag(x => x.Kittings, m =>
                                 {
                                     m.Table("AchievableKittings");
                                     m.Cascade(Cascade.Persist);
                                     m.Inverse(false);
                                     m.Key(k => 
                                     {
                                         k.PropertyRef(x => x.Uniqueidentifier);
                                         k.Column("aUniqueindentifier");
                                         k.ForeignKey("FK_Achievable_uniqueidentifier");
                                     });
                                 }, r => r.ManyToMany(m =>
                                                          {
                                                              m.Column("kittingId");
                                                              m.ForeignKey("FK_Kitting_kittingId");
                                                          }));
    }
}

public class KittingMap : ClassMapping<Kitting>
{
    public KittingMap()
    {
        Property(x => x.Name);

        Bag(x => x.Achievables, m =>
                                    {
                                        m.Table("AchievableKittings");
                                        m.Inverse(true);
                                        m.Key(k =>
                                                  {
                                                      k.Column("kittingId");
                                                      k.ForeignKey("FK_Kitting_kittingId");
                                                  });
                                    }, r => r.ManyToMany(m =>
                                                             {
                                                                 m.Column("aUniqueindentifier");
                                                                 m.ForeignKey("FK_Achievable_uniqueidentifier");
                                                             }));
    }
}

It seams that NHibernate ignore k.PropertyRef(x => x.Uniqueidentifier) statement. My Primary keys are bigint generated by identity. Now when Nhiebrnate tries to create the db its create the FK key for AchievableKittings to primaryKey of achievable, but not property-ref Uniqueidentifier

alter table AchievableKittings 
    add constraint FK_Achievable_uniqueidentifier 
    foreign key (aUniqueindentifier) 
    references Achievable

and of course its throws an exception:

Column 'Achievable.AchievableId' is not the same data type as referencing column 'AchievableKittings.aUniqueindentifier' in foreign key 'FK_Achievable_uniqueidentifier'.

Using FluentNhibernate is was able to map this in a right way. Unfortunately I can not use FluentNhibernate in current project.

So could some one help me with this issue.

isuruceanu
  • 1,157
  • 5
  • 23
  • looks like a bug to me. have you searched NH's bugtracker for this? – Firo Aug 14 '12 at 06:24
  • found only this reported bug (https://nhibernate.jira.com/browse/NH-1452) which looks similar to my issue. But in the reality it is a bug relater to Mapping ByCode as PropertyRef is converted wrongly into hbms (PropertyRef should be specified at ManyToMany level, not at bag one). I have an work around for this issue to use hbm mapping for Kiiting entity where property-ref is specified correctly into many-to-many specification. – isuruceanu Aug 16 '12 at 15:23

1 Answers1

0

I do it like this and always use sets

Set(x => x.Users, d =>
        {
            d.Schema("dbo");
            d.Inverse(false);
            d.Access(Accessor.Property);
            d.Table("UsersRoles");
            d.Key(c => c.Column("RoleId"));
        }, r => r.ManyToMany(c => { c.Class(typeof(User)); c.Column(n => n.Name("UserId")); }));
CrazyCoderz
  • 1,351
  • 1
  • 15
  • 30