NHibernate 父子映射
我在表 ACCOUNT 和表 USER 之间有父/子数据库关系。目前,我已经映射了如下所示的双向 Fluent 映射:
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");
在实践中,我无法预见我会想要从用户实体引用帐户实体。同样,我无法预见我想要从父帐户实体加载所有用户实体。我对 NHibernate 相当陌生,想知道上述方法是否仍然是提高性能的最佳方法?双向关系是首选吗?我应该只引用 Id 吗?谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Account
属性上使用延迟加载,那么它只会加载帐户的 ID。ListUsers
属性为inverse=true
,这意味着User
实体负责保存引用。因此,我相信(如果我没记错的话)ListUsers.Add(user);
行不是必需的,因为关联将由User
实体创建。< br>因此,这意味着您在添加用户时不必从数据库加载整个
ListUsers
集合。Account
property, then it would only load the account's id anyway.ListUsers
property isinverse=true
, meaning theUser
entity 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 theUser
entity.So this means that you don't have to load the entire
ListUsers
collection from the db when you add a user.