NHibernate data retrieve problem

时光总嘲笑我的痴心妄想 提交于 2019-12-25 04:56:14

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/3211552/nhibernate-data-retrieve-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!