0

我有课

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

}

我可以将我的数据保存在两个表中...但是在通过 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;
    }

sumbody 可以帮我解决这个问题....

4

1 回答 1

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;
于 2012-04-11T06:35:32.200 回答