NHibernate data retrieve problem

2019-08-22 23:20发布

These codes are working well when saving data.

But it is unable to retrieve data from b_TeacherDetail-table. For example:

TeacherRepository tRep = new TeacherRepository();
Teacher t = tRep.Get(12);

Here, t.TeacherDetail is null. But I know that there is an entry in the b_TeacherDetail-table for teacher-id 12.

Why?

My tables are:

Teacher {ID, Name, IsActive, DesignationID, DepartmentID}
TeacherDetail {ID, TeacherID, Address, MobileNo}

Teacher.cs

public class Teacher
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual bool IsActive { get; set; }        
        public virtual TeacherDetail TeacherDetail { get; set; }

        public virtual Designation Designation { get; set; }
        public virtual Department Department { get; set; }        
    }

TeacherDetail.cs

public class TeacherDetail
    {
        public virtual int ID { get; set; }
        public virtual Teacher Teacher { get; set; }
        public virtual string Address { get; set; }
        public virtual string MobileNo { get; set; }
    }

Teacher.hbm.xml

<class name="Teacher" table="b_Teacher">
    <id name="ID" column="ID">
      <generator class="native"/>
    </id>

    <property name="Name" column="Name" />
    <property name="IsActive" column="IsActive" />

    <one-to-one class="TeacherDetail" name="TeacherDetail" cascade="all" />

    <many-to-one name="Department" class="Department" unique="true" column="DepartmentID" />
    <many-to-one name="Designation" class="Designation" unique="true" column="DesignationID" />
  </class>

TeacherDetail.hbm.xml

<class name="TeacherDetail" table="b_TeacherDetail">
    <id name="ID" column="ID">
      <generator class="native"/>
    </id>

    <property name="Address" column="Address" />
    <property name="MobileNo" column="MobileNo" />

    <many-to-one name="Teacher" class="Teacher" column="TeacherID" unique="true" />
  </class>

Repository.cs

public class Repository<T> : IRepository<T>
    {
        ... ... ...

        public T Get(object id)
        {
            T obj = default(T);

            try
            {
                if (!_session.Transaction.IsActive)
                {
                    _session.BeginTransaction();
                    obj = (T)_session.Get<T>(id);
                    _session.Transaction.Commit();
                    _session.Flush();
                }
                else
                {
                    throw new Exception(CustomErrorMessage.TransactionAlreadyInProgress);
                }
            }
            catch (Exception)
            {
                _session.Transaction.Rollback();
                _session.Clear();

                throw;
            }

            return obj;
        }
... ... ... 
}

TeacherRepository .cs

public class TeacherRepository : Repository<Teacher>
    {
    }

2条回答
趁早两清
2楼-- · 2019-08-23 00:11

I think you should make Teacherdetail Many-to-one and not one-to-one

查看更多
神经病院院长
3楼-- · 2019-08-23 00:20

you are missing the reference to TeacherDetail from the Teacher point of view in your mapping. (nhibernate does not know how to fetch the entity)

So in Teacher.hbm.cml change the the to:

<one-to-one class="TeacherDetail" name="TeacherDetail" cascade="all" property-ref="Teacher" />

which tell it to fetch a TeacherDetail that has its Teacher property id value equal to this (Teacher) class's id value.

查看更多
登录 后发表回答