NHibernate выборка по полям присоединённой таблицы
От: Mullih  
Дата: 15.10.08 07:04
Оценка:
NHibernate выборка по полям присоединённой таблицы

Всем доброго времени суток

Собственно такая проблемка.
Если делать выборку по полю текущей таблицы то все хорошо, а если по полю присоединённой то говорит что не знает такого поля(
Что необходимо добавить или изменить
На всякий приатачил проект в VS 2008 с базой

Заранее благодарен

static void Main(string[] args)
{
    PlanRepository repository = new PlanRepository();
    List<Plan> plan1 = repository.GetPlansListRange(new DateTime(2008, 10, 13), new DateTime(2008, 10, 14));

    try
    {
        List<Plan> plan2 = repository.GetPlansListFuture(new DateTime(2008, 10, 14), 13, true, 5);
    }
    catch (Exception e)
    {
        Console.WriteLine(e); //could not resolve property: TimeId.StartTime of: ConsoleApplication1.Domain.Plan
    }
    Console.ReadKey();

}


Database MSSQL 2005

table Planning
Id int not null
Date datetime not null
TimeId int not null

table Time_table
ID_TIME_TABLE int not null
START bigint not null
FINISH bigint not null

public class PlanRepository
{

    protected ICriteria Criteria(ISession session)
    {
        ICriteria criteria = session.CreateCriteria(typeof(Plan), "Plan");
        criteria = criteria.CreateAlias("Plan.TimeId", "Plan_TimeId");
        return criteria;
    }

    public List<Plan> GetPlansListRange(DateTime from, DateTime to)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            ICriteria criteria =
                Criteria(session)
                    .Add(Restrictions.Between("Plan.Date", from, to));

            var result = criteria.List<Plan>();

            return new List<Plan>(result);
        }
    }

    public List<Plan> GetPlansListFuture(DateTime from, int periodNumber, bool isCurrentPeriod, int delta)
    {
        using (ISession session = NHibernateHelper.OpenSession())
        {
            ICriteria criteria =
                Criteria(session)
                    .Add(Restrictions.Between("Plan.Date", from, DateTime.Now.AddMonths(1).Date));

            if (isCurrentPeriod)
            {
                if (delta > 0)
                {
                    criteria =
                        criteria.Add(Restrictions.Gt("Plan.TimeId.StartTime", periodNumber - delta));
                }

                criteria = criteria
                    .Add(Restrictions.Gt("Plan.TimeId.FinishTime", periodNumber));
            }
            else
            {
                criteria = criteria
                    .Add(Restrictions.Gt("Plan.TimeId.StartTime", periodNumber));
            }
            var result = criteria.List<Plan>();
            return new List<Plan>(result);

        }
    }
}

public class NHibernateHelper
{
    private static readonly ISessionFactory _sessionFactory;
    private static readonly Configuration cfg;

    static NHibernateHelper()
    {
        try
        {
            cfg = new Configuration();
            cfg.Configure();
            cfg.AddAssembly(Assembly.GetExecutingAssembly());

            _sessionFactory = cfg.BuildSessionFactory();
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }

    public static ISessionFactory SessionFactory
    {
        get{return _sessionFactory;}
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}
public class Plan 
{
    public virtual long Id{get;set;}
    public virtual DateTime Date{get;set;}
    public virtual Time TimeId{get;set;}

}
public class Time 
{
    public virtual long Id{get;set;}
    public virtual long StartTime{get;set;}
    public virtual long FinishTime{get;set;}
    
}


mapping
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                                     assembly="ConsoleApplication1"
                                     namespace="ConsoleApplication1.Domain">

    <class name="Time" table="Time_table" lazy="true">
        <id name="Id" column="ID_TIME_TABLE">
            <generator class="identity"/>
        </id>
        <property name="StartTime" column="START" not-null="true"/>
        <property name="FinishTime" column="FINISH" not-null="true"/>
    </class>

    <class name="Plan" table="Planning" lazy="true">
        <id name="Id" column="Id">
            <generator class="identity"/>
        </id>
        <property name="Date" column="Date" not-null="true"/>
        <many-to-one name="TimeId" class="Time" column="TimeId" not-null="true" lazy="false"/>
    </class>


</hibernate-mapping>
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.