3

NHibernate mapping question. I have an entity called User and an entity called Menu. User contains two collections of menus.

public class User 
{
    public List<Menu> History {get; set;}
    public List<Menu> Favourites {get; set;}
}

public class Menu
{
    public string Name {get; set;}
    ...
}

Is there anyway I could, without creating new entity, generate two relationship tables for User and Menu (UserHistory and UserFavourites probably...), each contains mapping from UserIds to MenuIds? Can it be done with mappings only(FluentNHibernate mapping if possible)? Or is there a better way to do what I am trying to do here?

Thank you.

3 Answers3

2

this is fairly basic manytomany here.

public class UserMap()
{
    HasManyTomany(m => m.History).Table("UserHistory");
    HasManyTomany(m => m.Favourites).Table("Favourites").AsSet();
}

Depending on the context you should change List<Menu> to the appropriate interface

unordered but unique:

  • ISet<Menu> (.net4) / ICollection<Menu> (.net < 4)
  • add to hasmanytomany .AsSet()

ordered by added:

  • IList<Menu>
  • add to hasmanytomany AsList("indexcolumnName") which will automaticly add and maintain an index column to the intermediate tables
Community
  • 1
  • 1
Firo
  • 30,626
  • 4
  • 55
  • 94
2

I'd use

public class UserMap : ClassMap<User>
{
     References(m => m.History).Column("HistoryId");
     References(m => m.Favourites).Column("FavouritesId");   
}

in users

and sub class Menu for UserHistory and UserFavourites.

public class MenuMap : ClassMap<Menu>
{
  public ParentMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);

    DiscriminateSubClassesOnColumn("type");
  }
}
public class ChildMap : SubclassMap<Child>
{
  public ChildMap()
  {
    Map(x => x.AnotherProperty);
  }
}
Sly
  • 15,046
  • 12
  • 60
  • 89
Johnno Nolan
  • 29,228
  • 19
  • 111
  • 160
  • Thank you John. Would this solution create extra record in table Menu with a discriminator column and value of UserHistory/UserFavourites? I was hoping there is a solution only involves creating records in link tables but not the menu table. History and Favourite are both Menu and there is no extra field for any of them... – user1140323 Jan 10 '12 at 11:56
  • Think I misunderstood your question. This would create an extra record as you described. – Johnno Nolan Jan 11 '12 at 09:16
  • Thanks anyway. I am not sure if the stuff I am trying to do is achievable...Maybe creating new record or a new entity is the only way to do it... – user1140323 Jan 12 '12 at 03:13
-1

Have you tried to use two separate tables in your mappings:

this.References(x => x.Favorites).Table("Favorites");
this.References(x => x.History).Table("History");
Toni Parviainen
  • 2,217
  • 1
  • 16
  • 15
  • The table is ignored when referencing an entity. It is only used for value collections (element), component collections (composite-element) and many-to-many relations. – Stefan Steinegger Jul 16 '12 at 09:55
  • this even does not Compile because References returns a builder object which does not has a method Table() – Firo Jul 16 '12 at 12:49