有一个实体的的 A 的
There's an entity A.
此外,还有一个实体的的 B 的有两个协会是的 A 的
In addition, there's an entity B which has two associations with A.
A 的有一个集合的的 B 的
A has a collection of B.
本集合必须加载任何的的 B 的如果一个相关的的 A 的是的父加载的 A 的
This collection must load any B if one of associated A is the parent of loaded A.
但问题是集合映射在的 A 的必须按照过滤器的孩子检查,如果一两个 A 的的关联是父之一。
Problem is collection mapping on A must filter children based on checking if one of two A associations is the parent one.
我怎样才能做到这一点?
How can I achieve that?
注:顺序并不重要,这样你就可以使用提出一些映射的包 的
注2:请说明如何实现,使用XML映射,我不会做,在code 的
更新:真实的情况:
这是所有关于友谊的实现。我要地图在实体用户配置友谊的集合。友谊两会重新presenting的关系:OneUser,OtherUser。如果我想要得到的所有朋友对我来说,我需要检查这两个特性,因为一个是我的朋友,如果这两个属性之一,是我自己。的
如果你愿意稍微移动解决方案出来的数据域,用户可以有他们分配为其他用户一到一对多的关系朋友。这可能是保护
或私人
如果你不希望它泄漏出去。
IF you are willing to move the solution slightly out of the data domain, your users could have a one-to-many relationship of other users they have assigned as friends. This could be protected
or private
if you don't want it to leak out.
然后,执行定义一个实际的朋友作为需要在两个方向的关系的公共财产。也许是这样的:
Then, you implement a public property that defines an actual Friendship as requiring the relationship in both directions. Maybe something like:
public List<User> Friends {
this.assignedFriends.Where(f => f.assignFriends.Contains(this)).ToList();
}
你要确保你使用的是二级缓存的一种形式,如果你这样做然而,否则要求该属性将捶你的数据库。
You'll want to make sure you are using a form of 2nd level cache if you do this however, otherwise requests to this property will hammer your database.
相反,如果你需要返回一个专门的友谊
对象的集合,它取决于你是如何坚持这些条目,你是如何映射这个类。
If instead you need to return a collection of a dedicated Friendship
object, it depends on how you are persisting those entries and how you have mapped that class.
一种选择是定义友谊
来只是有三列,一个主键,以及两个外键回到用户
表。然后,您需要逻辑一点点,每当有人试图添加好友:首先需要检查是否已经存在一半的关系。
One option would be to define Friendship
to simply have three columns, a primary key, and two foreign keys back to the User
table. You then require a little bit of logic whenever someone tries to add a friend: you first need to check if half of the relationship already exists..
var f = session.Query<Friend>()
.Where(x => x.User1 == YourCurrentUser)
.Cacheable()
.FirstOrDefault();
然后,如果它存在分配的关系的另一半
then if it exists you assign the other half of the relationship
f.User2 = TheOtherUser;
否则,创建一个新的对象:
otherwise create a new object:
f = new Friendship();
f.User1 = YourCurrentUser;
和任何分支后:
session.SaveOrUpdate(f);
那么用户
的朋友变成
public property List<Friendship> {
session.Query<Friendship>()
.Where(f =>
f.User1 != null && f.User2 != null
&& (f.User1 == this || f.User2 == this)
).Cacheable()
.ToList();
}
请注意,我不是我的安装VS.近请原谅错别字,或者如果我做了明显的浮躁错误(这是凌晨2:30我在哪里presenltly)的