nhibernate中的生效日期
我在两个现有表User:UserId、Name和Location:LocationId、Name之间有一个多对多关系,这些表不能更改。我使用了多对多映射,但我们有一个新的要求,包括历史/未来运动 我使用了peoplesoft的概念,即在一个新的intersect表UserLocation中对记录进行有效的日期测定:UserId、LocationId、EffectiveDate、Status,它们替换了旧表,其中前3个字段构成主键 在SQL中,我可以简单地使用以下语句返回给定生效日期的活动记录,但我无法从NHibernate获得类似的内容,甚至无法获得有关参数在多级选择中不起作用的错误nhibernate中的生效日期,nhibernate,date,many-to-many,mapping-by-code,Nhibernate,Date,Many To Many,Mapping By Code,我在两个现有表User:UserId、Name和Location:LocationId、Name之间有一个多对多关系,这些表不能更改。我使用了多对多映射,但我们有一个新的要求,包括历史/未来运动 我使用了peoplesoft的概念,即在一个新的intersect表UserLocation中对记录进行有效的日期测定:UserId、LocationId、EffectiveDate、Status,它们替换了旧表,其中前3个字段构成主键 在SQL中,我可以简单地使用以下语句返回给定生效日期的活动记录,但
select l.*
from location l
where l.locationId in
(select ul.locationId
from UserLocation ul
where (ul.EffectiveDate =
(select max(ul_ed.EffectiveDate)
from UserLocation ul_ed
where ul.LocationId = ul_ed.LocationId
and ul.EffectiveDate = ul_ed.EffectiveDate
and ul.UserId - ul_ed.UserId
and ul.Status = true))
我需要通过包含UserId和EffectiveDate列的参数来返回进一步过滤的记录,但我很乐意返回所有内容
然而,我的问题是我使用代码映射的映射类不流畅,甚至无法接近有限的NHibernate知识。我希望不必为intersect表创建新类。我甚至还没有到插入/更新/删除部分,因为我甚至不能让选择工作
要求:
1.将用户关联到具有EffectiveDate=NOW、Status=True的位置时,在UserLocation中插入新记录
3.将用户移动到新位置时,应在UserLocation中插入新记录,有效日期为NOW,状态为True
4.从位置删除用户将插入新的UserLocation记录,其生效日期=现在,状态=False
5.检索用户在给定日期关联的位置
注意:由于报告是针对该数据生成的,因此我们无法删除记录,并且还会为将来的事件创建数据
如果有人能为我指出正确的开始方向,我将不胜感激。我已经研究了Loader和Interceptor类,但似乎无法使其合适。您需要为intersect表创建一个类,因为它有两个额外的属性。第一个需求可以像这样实现
public virtual void AddLocation(Location l)
{
this.UserLocations.Add(new UserLocation
{
EffectiveDate = DateTime.Now(),
Status = true,
User = this,
Location = l
});
}
查询应该有点直截了当。只需将Alias加入intersect表并比较EffectiveDate。这是我们为所述场景找到的答案
Filter("EffectiveDate", mm => mm.Condition("Status = 1 and EffectiveDate = (select max(t1.EffectiveDate) from Table1 t1 where t1.Id = Id and t1.EffectiveDate <= :effectiveDate)"));
过滤器可以应用于表和映射。但是,我们最终将这两个表合并到一个表中以降低复杂性
注意:必须先定义筛选器并将其应用于会话,然后才能在映射中使用它。请参阅nHibernate doco,以了解它的完整详细信息。我终于使用映射类的过滤器功能找到了答案。我们还必须使EffectiveDate和Status字段成为同一个表的一部分,以使其正常工作。