Nhibernate:具有 HasMany 的组件
我有这样的映射:
public sealed class EntityMap : ClassMap<Entity>
{
public EntityMap ()
{
...
Component(entity => entity.StateHistory,
m => m.HasMany<HistoryItem<EntityState>>
(Reveal.Property<EntityStateHistory>("Items"))
.Table("EntityStateHistory")
.KeyColumn("IDEntity")
.Component
( m2 =>
{
m2.Map(esh => esh.Item, "State")
.CustomType(typeof(EntityState)).Not.Nullable();
m2.Map(esh=> esh.Date, "TransitionDate").Not.Nullable();
}
)
.Cascade.AllDeleteOrphan());
...
}
}
我想进行查询,在 EntityStateHistory 条目中获取特定日期 (TransitionDate)。 如果可能的话,会是这样的:
“select e from Entity e where e.StateHistory.Items.Date = :date”
但我不能这样做,我不知道如何访问历史记录日期,知道 History 是一个组件,它本身有一个组件集合,并且我需要访问集合中这些组件的属性之一。
对象模型是这样的:
public class Entity
{
private int ID {get; set;}
etc
...
public virtual EntityStateHistory StateHistory{ get; private set; }
}
public class EntityStateHistory: History<EntityState>
{
//some wraped properties and methods
public IList<HistoryItem<EntityState>> StateRecords
{
get { return base.Items;}
}
public bool ContainsStateRecord(EstadoOT state)
{
return base.Items.Count(i => i.Item.Equals(state)) > 0;
}
etc ...
}
public class History<T>
{
protected virtual IList<HistoryItem<T>> Items { get; private set; }
public History()
{
Items = new List<HistoryItem<T>>();
}
protected virtual HistoryItem<T> AddHistoryItem(DateTime data, T item)
{
...
}
}
public class ItemHistory<T>
{
#region NHibernate
private int ID { get; set; }
#endregion
public virtual DateTime Date { get; private set; }
public virtual T Item { get; private set; }
...
}
我知道我可能必须更改实体的映射,并为 EntityStateHistory 创建一个映射,但我想避免这种情况,因为这意味着多一个表。我的方式是最规范的映射,因为不需要映射 HistoryItem 或 EntityStateHistory,这意味着我只使用一张表来映射 EntityStateHistory:
Table EntitiStateHistory: -身份 - 过渡日期 -State
那么当前映射是否可以在数据库中查询具有特定历史记录日期的实体?
谢谢
I have this mapping:
public sealed class EntityMap : ClassMap<Entity>
{
public EntityMap ()
{
...
Component(entity => entity.StateHistory,
m => m.HasMany<HistoryItem<EntityState>>
(Reveal.Property<EntityStateHistory>("Items"))
.Table("EntityStateHistory")
.KeyColumn("IDEntity")
.Component
( m2 =>
{
m2.Map(esh => esh.Item, "State")
.CustomType(typeof(EntityState)).Not.Nullable();
m2.Map(esh=> esh.Date, "TransitionDate").Not.Nullable();
}
)
.Cascade.AllDeleteOrphan());
...
}
}
I want to make a query where i get a specific date (TransitionDate) in an EntityStateHistory entry.
If it was possible it would be something like this:
"select e from Entity e where e.StateHistory.Items.Date = :date"
but i can't do this, i don't know how i can access an History record date, knowing that History is a component that has itself a collection of components, and i need to access one of the properties of those components in the collection.
The object model is something like this:
public class Entity
{
private int ID {get; set;}
etc
...
public virtual EntityStateHistory StateHistory{ get; private set; }
}
public class EntityStateHistory: History<EntityState>
{
//some wraped properties and methods
public IList<HistoryItem<EntityState>> StateRecords
{
get { return base.Items;}
}
public bool ContainsStateRecord(EstadoOT state)
{
return base.Items.Count(i => i.Item.Equals(state)) > 0;
}
etc ...
}
public class History<T>
{
protected virtual IList<HistoryItem<T>> Items { get; private set; }
public History()
{
Items = new List<HistoryItem<T>>();
}
protected virtual HistoryItem<T> AddHistoryItem(DateTime data, T item)
{
...
}
}
public class ItemHistory<T>
{
#region NHibernate
private int ID { get; set; }
#endregion
public virtual DateTime Date { get; private set; }
public virtual T Item { get; private set; }
...
}
I know i will probably have to change the mapping for entity, and create a map for EntityStateHistory, but i would like to avoid that, because that means one more table. The way i have it is the most canonical mapping because has no need to map HistoryItem or EntityStateHistory, that means i only use one table to map EntityStateHistory:
Table EntitiStateHistory:
-IDEntity
-TransitionDate
-State
So is it possible with the current mapping to query the database for a Entity that has a specific history record date?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
太容易了...
问题出在路径上:
“select e from Entity e where e.StateHistory.Items.Date = :date”
如果我这样做:
“select e from Entity e join e.StateHistory.Items i where i.Date = :date”
它有效
It was so easy...
The problem was in the path:
"select e from Entity e where e.StateHistory.Items.Date = :date"
if i do this:
"select e from Entity e join e.StateHistory.Items i where i.Date = :date"
it works