-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Amazing association
PostPosted: Tue May 16, 2006 7:58 pm 
Newbie

Joined: Tue May 16, 2006 7:48 pm
Posts: 5
Dear Friens, i work with Hibernate for two years,but i never found something that i wasn't capable to fix it.
But this time, i have problemns.
I have the follow legacy model:

Table1: (Search)
PK: searchid.

Table2: (Questions)
PK: searchid, questionNumber(questionnumber is not a sequence, is only a number to identify the question in the Search. The value is always 1, 2 or 3)

Table 3:(Answers)
PK: searchid,questionNumber,answerid

I can´t make an association that works. :(


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 16, 2006 9:52 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
<class name="Search">
  <id name="SearchID">
    ...
  </id>

  <list name="Questions">
    <key column="SearchID"/>
    <list-index column="questionNumber" base="1"/>
    <one-to-many class="Question"/>
  </list>
</class>

<class name="Question">
  <composite-id name="QuestionID" class="QuestionCompID">
    <key-many-to-one name="Search" column="SearchID"/>
    <key-property name="Number" column="questionNumber"/>
  </composite-id>

  <set name="Answers">
    <key column="AnswerID"/>
    <one-to-many class="Answer"/>
  </set>
</class>
Is this close to what you want?

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 9:37 am 
Newbie

Joined: Tue May 16, 2006 7:48 pm
Posts: 5
Thank you for the reply my friend.
So, i must to have a property type of Search in the class QuestionCompID?
How can i get the Search associated to the Question? throught the property in QuestionCompID?

So i would have something like that: question.getQuestionId().getSearch()
that´s it?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 9:46 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
The approach question.getQuestionId().getSearch() will work fine. There is other approach as well.

Code:
<class name="Question">
   <composite-id>
      <key-property name="Search" column="SearchID"/>
      <key-property name="Number" column="questionNumber"/>
   </composite-id>

   <many-to-one name="searchObject" column="SearchID" insert="false" update="false" class="Search"/>

   <set name="Answers">
      <key column="AnswerID"/>
      <one-to-many class="Answer"/>
   </set>
</class>


In this case I will have a simple class QuestionPK with two properties( Search, Number ) and have your mapping class Question extend QuestionPK. Hibernate just needs to know your mapping class information i.e. Question and doesnt need to know anything about QuestionPK. Make sure to have QuestionPK implement Serializable interface.

question.getSearch()
question.getSearchObject()

first one gives you only ID information.
second one gives you reference to object.

Check this link also http://www.hibernate.org/117.html#A34


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 10:11 am 
Newbie

Joined: Tue May 16, 2006 7:48 pm
Posts: 5
Hi friend, i understand your point, and i think that is ok for association among search and question.
But the association among question and answer i think that is not another way, we must to use key-many-to-one in the composite-id. Or no?

Regards,

Márcio Di Pietro.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 17, 2006 10:53 am 
Newbie

Joined: Tue May 16, 2006 7:48 pm
Posts: 5
I resolved the problem. For the relation among Question and Answer i use this:

<set name="answer" inverse="true">
<key>
<column name="questionID"/>
<column name="searchID"/>
</key>
<one-to-many class="Answer"/>
</set>

Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.