views:

154

answers:

1

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

A: 

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

Miguel Marques