views:

65

answers:

2

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

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

chrissie1
Is that actually possible without rearranging my table-columns?
JMSA
Oops sorry I made a misstake, I will edit my answer. I just now saw your table structure.
chrissie1
+2  A: 

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.

Jaguar
Jaguar is correct. If you don't specify a `property-ref`, NHibernate will try to match the `id`s, which is not correct in this case.
Diego Mijelshon
I have deleted and recreated the tables in my database. And that positively solved my problem. Can you tell me what happened?
JMSA
You have not solved your problem, recreating tables means that the Id Seeds (generator="native" == sql-servers Identity ?) have been reset to 1. So as long you insert to both tables at the same time the Id's of the tables for each pair of Teacher will match. When that condition breaks your problem will appear again
Jaguar