I have a parent/child database relationship between the table ACCOUNT and the table USER. Currently I have mapped a bi-directional Fluent mappings like this:
public class Account {
public virtual IList<User> ListUsers { get; private set; }
public virtual void AddUser(User user)
{
user.Account = this;
ListUsers.Add(user);
}
}
MAPPING: HasMany(x => x.ListUsers).Table("UserInfo").KeyColumn("Id_Account").Inverse().Cascade.All().AsList();
public class User {
public virtual Account Account { get; set; }
public string Username { get; set; }
}
MAPPING: References(x => x.Account).Column("Id_Account");
In practice I cannot foresee that I will ever want to reference the Account entity from the User entity. Likewise I cannot foresee my wanting to load all of the User entities from the parent Accounts entity. I am fairly new to NHibernate and was wondering is the above method still the best way to go performance wise? Is a bi-directional relationship preferred and should I look to referencing the Id only? Thanks
Accountproperty, then it would only load the account’s id anyway.ListUsersproperty isinverse=true, meaning theUserentity is responsible for saving the reference. Therefore, I believe (if I remember correctly) that the lineListUsers.Add(user);is not necessary, since the association will be created by theUserentity.So this means that you don’t have to load the entire
ListUserscollection from the db when you add a user.