To make a primary key in one class as foreign key in another class using mapping file of nhibernate?

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.

Up vote 0 down vote favorite share g+ share fb share tw.

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... nhibernate nhibernate-mapping link|improve this question edited Mar 22 at 14:56CodeWorks2,1911531 asked Mar 22 at 10:57user1274646195 36% accept rate.

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 _answers; //other props public virtual int ID { get{ return _id; } set{ _id = value; } } public virtual ISet Answers { get { return _answers; } set { _answers = value; } } } Mapping for the Question 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.

Hey firstly thanx 4 such a big code...and takin so much efforts..Can you 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 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 at 12:28 Can you please go through the following link stackoverflow.com/questions/9792086/… 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 at 13:06 I have viewed your other question. Your Code seems to work, but I don't 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 at 14:00.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions