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

本文关键字:数据 获取 HQL | 更新日期: 2023-09-27 18:30:46

我有一个类

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; }

}

爱好里面的细节:

  [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; }

}

我可以将我的数据保存在两个表中...但是在通过爱好家庭ID获取爱好细节时...我收到错误,因为找不到字段

爱好之家的映射文件:

<?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;
    }

可以总结请帮我解决这个问题....

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

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

您可以将从 HobbyDetail 添加到 HobbyHome 的反向引用,使引用是双向的。

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

另一方面,加载爱好之家要容易得多。

        ICriteria criteria = session.CreateCriteria(typeof(HobbyHome))
           .Add(Restrictions.IdEq(id));
        var home = criteria.UniqueResult<HobbyHome>();
        var details = home.HobbyDetailList;