无法使用HQL通过外键从表中获取数据

时间:2012-04-11 05:22:15

标签: c# nhibernate hql

我有一个班级

public class HobbyHome:Establishment

{

public virtual ICollection<HobbyHomeAddress> HobbyHomeAddressList { set; get; }
public virtual ICollection<HobbyDeveloper> HobbyDeveloperList { set; get; }
public virtual ICollection<HobbyDetail> HobbyDetailList { set; get; }

}

在hobbyDetail里面:

  [Key]
    public virtual int HobbyDetailID { get; set; }


   // public virtual int HobbyId { get; set; }     
    public virtual HobbyMasters Hobbymaster { get; set; }
    public virtual Course course { get; set; }
    public virtual StudyMedium StudyMedium { get; set; }
    public virtual decimal Fees { get; set; }

}

ican在两个表中保存我的数据...但是在通过HobbyHomeId获取hobbydetail时......我收到错误,因为找不到字段

Hobbyhome的映射文件:

<?xml version="1.0" encoding="utf-8" ?>

 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="HobbyHomes"  namespace="HobbyHomes.Model" >
  <class name ="HobbyHome" table="HobbyHome">
   <id name="EstablishmentId" column="HobbyHomeId">
        <generator class="native"/>
   </id>
  <property name="Name"/>

  <set name="HobbyHomeAddressList">
        <key column="HobbyHomeId" foreign-key="fk_HobbyHomeAddress_HobbyHomeId"/>
      <one-to-many class ="HobbyHomes.Model.HobbyHomeAddress"/>
      </set>
      <set name="HobbyDeveloperList">
           <key column="HobbyHomeId" foreign-key="fk_HobbyDeveloper_HobbyHomeId"/>
           <one-to-many class ="HobbyHomes.Model.HobbyDeveloper"/>
   </set>
   <set name="HobbyDetailList">
       <key column="HobbyHomeId" foreign-key="fk_HobbyDetail_HobbyHomeId"/>
       <one-to-many class ="HobbyHomes.Model.HobbyDetail"/>
  </set>
 </class>
</hibernate-mapping>

这是我获取数据的函数:

  public HobbyDetail FetchbyHobbyId(int id)
    {
        log.Debug("Start");
        ISession session = DataAccessLayerHelper.OpenWriterSession();
        ITransaction transaction = session.BeginTransaction();
        HobbyDetail hobbydetail = null;
        try
        {

            ICriteria criteria = session.CreateCriteria(typeof(HobbyDetail))
               .Add(Restrictions.Eq("HobbyHomeId", id));

            hobbydetail = criteria.UniqueResult<HobbyDetail>();

            transaction.Commit();
        }
        catch (SessionException ex)
        {
            if (transaction != null && transaction.IsActive)
                transaction.Rollback();

            log.Error(ex);
            hobbydetail = null;
        }

        finally
        {
            if (transaction != null)
                transaction.Dispose();

            if (session != null && session.IsConnected)
                session.Close();

            log.Debug("End");
        }
        return hobbydetail;
    }

总结请帮助我....

1 个答案:

答案 0 :(得分:0)

HQL是一种面向对象的查询语言。您只能使用类模型中存在的标识符。

您可以将HobbyDetail的后向引用添加到HobbyHome,使引用成为双向。

您还可以使用子查询来获取HobbyHome列表中的所有详细信息。使用HQL比使用标准更容易。

另一方面,加载HobbyHome要容易得多。

        ICriteria criteria = session.CreateCriteria(typeof(HobbyHome))
           .Add(Restrictions.IdEq(id));

        var home = criteria.UniqueResult<HobbyHome>();
        var details = home.HobbyDetailList;