I have recently inherited a project at work that contains NHibernate. I am extremely new to it and have to make a modification to one of the mappings. I\'ve read through the d
Well, your mapping seems to be correct, i.e. already returning the ProjectName
. To be sure please, check that the object Project
is mapped like this:
And the Project would be like this
public class Project
{
public virtual int Id { get; set;}
public virtual string Name { get; set;}
public virtual IList Users { get; set;}
...
So, having this in place, we should be able to use the User:
public class User
{
public virtual IList Projects { get; set;}
...
mapped and loaded by NHibernate like this
user = session.Get(x) // id of searched user
foreach(var project in user.Projects)
{
var name = project.Name;
var id = project.Id;
...
}
NOTES:
In case of many-to-many
there obviously could/should be
mapping on both sides - Project and User. But only one of them can have inverse="true"
. So in this case Project.Users
does not have that.
The cascade
setting on many-to-many
is doing (most likely) different thing than one would expect. It is not related to the pairing table but to the second end of that mapping.
Cascading of the pairing object is done out of the box. It does not have to be mapped, in fact it cannot be mapped or turned off... Other words I would suggest to remove that cascade, unless you really want to change the Project in persistence, if you are working with some User.
Check also: