0

Hi i am developing an application in MVC using n-hibernate The application is AskQuestion forum where on the First Page a list of questions are displayed and on click of these questions another page opens which shows the answers of the question.. This is my table Structure: Question Table:

QuestionID int
Question nvarchar(255)
Created_Date datetime
Modified_Date datetime
Created_By int
Modified_By int
Deleted nchar(1)

Answer Table:

AnswerId int
Answer nvarchar(255)
Created_Date datetime 
Modified_Date datetime  
Created_By int 
Modified_By  int
QuestionID int 
Deleted  nchar(1)

Now i want to create a mapping between them so that primarykey QuestionID in Question table becomes foreign key in Answer table using Mapping class(XML File) Please anyone help me...

user1274646
  • 921
  • 6
  • 21
  • 46

1 Answers1

0

Here is some code that may can help you. I have written to classes and a mapping for each one. I had only defined the propertys QuestionID and AnswerID. In the Question class you have now a ISet, which contains all referenced Answers. In the Answer class you have a Questionfield, which contains the referenced Question.

Hope that is what you want (Sample Code is in C#)

Class for the Question

public class Question
    {
        //other fields
        private int _id;
        private ISet<Answer> _answers;

        //other props
        public virtual int ID
        {
            get{ return _id; }
            set{ _id = value; }
        }

        public virtual ISet<Answer> Answers
        {
            get
            {
                return _answers;
            }
            set
            {
                _answers = value;
            }
        }
    }

Mapping for the Question

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="App.Question, App" table="question">
    <id name="ID" column="questionid" type="int">
      <generator class="native" />
    </id>

    <!--Other Propertys-->

    <set name="Answers" table="answer" generic="true" inverse="true">
      <key column="questionId" />
      <one-to-many class="App.Answer, App"/>
    </set>
  </class>
</hibernate-mapping>

Class for the Answer

public class Answer
    {
        //other fields
        private int _id;
        private Question _question;

        //other props
        public virtual int ID
        {
            get{ return _id; }
            set{ _id = value; }
        }

        public virtual Question Question
        {
            get
            {
                return _question;
            }
            set
            {
                _question = value;
            }
        }
    }

Mapping for the Answer

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="App.Answer, App" table="answer">
    <id name="ID" column="answerid" type="int">
      <generator class="native" />
    </id>

    <!--Other Propertys-->

    <many-to-one name="Question" column="questionId" class="App.Question, App"/>
  </class>
</hibernate-mapping>
Andre Gross
  • 263
  • 2
  • 8
  • hey firstly thanx 4 such a big code...and takin so much efforts..Can u tell me hw to access the id's of both the classes coz that is the problem i was facing with my current code..where i had to fire a query to compare the values contained in both the columns – user1274646 Mar 22 '12 at 12:24
  • Ok, I hope I understand you now right. A little Example you have an object of an Question. (For Example session.CreateCriteraia(typeof(Question)).Add(new EqExpression("ID", 1))... Now you can access the Answers of the Question over the Property 'Answers'. Hibernate automatically find the correct Answers. If you have mapped the two Classes correctly. – Andre Gross Mar 22 '12 at 12:28
  • Can u please go through the following link http://stackoverflow.com/questions/9792086/to-insert-a-value-in-foreign-key-in-mvc3-nihibernate and check whethr i have done correct or not and then plzzz also tell me where should i put the above code like in which function of the Nhibernate Helper class – user1274646 Mar 22 '12 at 13:06
  • I have viewed your other question. Your Code seems to work, but I dont understand the last function. You have to do 2 updates in your code. Th first one is, that you add the 'Answers'-Property to your QuestionPage, the second is in the question_page-Mapping, there you have to add the mapping for the 'Answer'-Property. For the Code show my first and second codeblock. If you done this, you can do what you want: If you click on the selected question you can show up the answers referenced to the question, which are in the 'Answers'-Prop of your question-object. I think that will solve your problem – Andre Gross Mar 22 '12 at 14:00