Nhibernate:具有 HasMany 的组件

发布于 2024-08-20 11:55:12 字数 2496 浏览 3 评论 0原文

我有这样的映射:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-08-27 11:55:12

太容易了...
问题出在路径上:
“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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文