这就是我将如何做到的。
用户将包含一个列表(位置)和对当前位置(源)的引用,因此您可以获得每个用户的当前位置和用户位置的历史列表。
默认情况下,User.Locations 和 User.Source 都将延迟加载,但您可以使用任何查询选项来急切加载 User.Source 以获取当前位置,以便您受益。
当您通过 Locations 属性向用户添加位置时,您显然还需要管理源引用。
如果您想要我可以提供的 XML 映射文件,以及我使用 Fluent NHibernate 1.1。
public class User
{
public virtual int UserId { get; set; }
public virtual int GroupId { get; set; }
public virtual IList<Location> Locations { get; set; }
public virtual Location Source { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as User;
if (t == null)
return false;
if (UserId == t.UserId && GroupId == t.GroupId)
return true;
return false;
}
public override int GetHashCode()
{
return (UserId + "|" + GroupId).GetHashCode();
}
}
public class Source
{
public virtual int Id { get; set; }
}
public class Location
{
public virtual User User { get; set; }
public virtual int Id { get; set; }
public virtual Source Source { get; set; }
public virtual string X { get; set; }
public virtual string Y { get; set; }
public override bool Equals(object obj)
{
if (obj == null)
return false;
var t = obj as Location;
if (t == null)
return false;
if (User == t.User && Source == t.Source)
return true;
return false;
}
public override int GetHashCode()
{
return (User.GetHashCode() + "|" + Id).GetHashCode();
}
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
CompositeId()
.KeyProperty(x => x.UserId, "UserId")
.KeyProperty(x => x.GroupId, "GroupId");
HasMany(x => x.Locations);
References(x => x.Source).Columns("UserId", "GroupId", "LocationSource");
}
}
public class LocationMap : ClassMap<Location>
{
public LocationMap()
{
CompositeId()
.KeyReference(x => x.Source, "Source")
.KeyReference(x => x.User,"groupId","userid");
References(x => x.User).Columns("userid","groupid");
}
}
public class SourceMap : ClassMap<Source>
{
public SourceMap()
{
Id(x => x.Id).GeneratedBy.Native();
}
}