Please help! I couldn’t figure it out how to map the following situation:
I have only 1 table.
[Table] User { id, name }
My class look like this
public class User { public int Id { get; set; } public string Name { get; set; } public ISet<User> Friends { get; set; } }
Each user has relationship with other users. e.g.’User A’ can has many friends which is other User.
What should be the mapping for this? I think this should be Many-to-Many relationship but I don’t really know how the HBM will look like?
Thanks,
You are correct. In your scenario you will need to use a self-referential many-to-many mapping. But using a single Users table you cannot represent the relation between users and friends (using a single table you could represent a self-referential parent-child relationship). You will need an intermediary table to achieve this. Here’s an example using SQLite ADO.NET provider to show one possible way of modeling your scenario:
User.hbm.xml:
From the above mapping you will notice the use of the following tables: Users and Friends
And here’s the code:
And finally for the sake of completeness here’s my config file: