简体   繁体   English

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

[英]can not fetch data from table through foreign key using HQL

i have a class我有一个 class

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

} }

inside the hobbyDetail: hobby里面的细节:

  [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 save my data in both the tables...but while fetching hobbydetail through HobbyHomeId...i'm getting error as can not find Field我可以将我的数据保存在两个表中...但是在通过 HobbyHomeId 获取 hobbydetail 时...我收到错误消息,因为找不到字段

the mapping file for Hobbyhome: 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>

here is my function for fetch the data:这是我的 function 用于获取数据:

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

can sumbody please help me with this.... sumbody可以帮我解决这个问题吗....

HQL is an object oriented query language. HQL是一种面向object的查询语言。 You can only use identifiers that exist in your class model.您只能使用 class model 中存在的标识符。

You could add a back reference from the HobbyDetail to the HobbyHome making the reference bidirectional.您可以添加从 HobbyDetail 到 HobbyHome 的反向引用,使引用成为双向的。

You could also use a subquery to get all the details that are in the list of the HobbyHome.您还可以使用子查询来获取 HobbyHome 列表中的所有详细信息。 This is easier with HQL than with criteria.使用 HQL 比使用标准更容易。

On the other hand it is much easier to just load the HobbyHome.另一方面,只加载 HobbyHome 要容易得多。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM