1

嗨,我想在表之间执行连接,但我无法做到。

这是我的课程:

 public class Person
{
   public virtual LoginAccount LoginAccount { get; set; }
}

 public class LoginAccount 
{
 //fields of LoginAccount
}

Person的映射文件

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly ="HobbyHomes" namespace ="HobbyHomes.Model" >
<class name ="Person">

<composite-id class="HobbyHomes.Model.LoginAccount" name="LoginAccount">
  <key-property name="LoginAccountID">
    <column name="LoginAccountID"></column>
  </key-property>
</composite-id>

//other properties

 </class>

现在这是我试图从 Person 获取 LoginAccount 值的内容:

  public IList<Person> FetchPerson(ICollection<Learner> learner)
    {
        log.Debug("Started");
        ISession session = DataAccessLayerHelper.OpenReaderSession();
        IList<Person> person = null;
        ITransaction transaction = null;
        Disjunction dj = new Disjunction();
        try
        {
            transaction = session.BeginTransaction();


            person = session.CreateCriteria(typeof(Person), "person")
                            .CreateCriteria("person.LoginAccount", "LoginAccount", NHibernate.SqlCommand.JoinType.LeftOuterJoin)
                            .List<Person>();
            transaction.Commit();


        }
        catch (Exception ex)
        {
            if (transaction != null && transaction.IsActive)
                transaction.Rollback();
            log.Error(ex);

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

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

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

但是这样我只能获取 Person 类的字段,而不是 LoginAccount 类的字段。请帮助我。

4

1 回答 1

0

person = session.CreateCriteria(typeof(Person), "person").List<Person>()应该为您提供所有属性,因为该列LoginAccountID是人员表的一部分。

也许你想要的是一个关键参考

<composite-id>
  <key-many-to-one class="HobbyHomes.Model.LoginAccount" name="LoginAccount">
    <column name="LoginAccountID"></column>
  </key-many-to-one>
</composite-id>
于 2012-05-14T12:39:48.797 回答