How to get the parent TO from the child

0 views
Skip to first unread message

James Allen

unread,
May 11, 2008, 1:15:56 PM5/11/08
to transfer-dev
Hi guys,

I'm probably missing something obvious but can't seem to find a way to
retrieve the parent object from it's child.

To be clearer, I have a question object which has a onetomany to it's
answers.

Therefore, when I create an answer TO I am able to
setParentQuestion(question) to attach it to it's parent.

However, I don't seem to be able to retrieve the question object from
an individual answer.

If I call answer.getParentQuestion() on an answer TO (obtained via
transfer.get) I get this error:

A OneToMany Parent TransferObject has not been initialised.
In TransferObject 'question.Answer' onetomany parent
'question.Question' is not set.

Now, normally I would have a relationship from answer back to question
but this won't work as it will create the dreaded multiple select
fields error (due to the onetomany to answers on question).

I'm sure this is simple but in this case how do I retrieve the parent
question from an individual answer?

Thanks in advance,
James.

Stephen Moretti

unread,
May 11, 2008, 3:09:02 PM5/11/08
to transf...@googlegroups.com


2008/5/11 James Allen <sling...@googlemail.com>:
--~--~---------~--~----~------------~-------~--~----~
Before posting questions to the group please read:
http://groups.google.com/group/transfer-dev/web/how-to-ask-support-questions-on-transfer



James  - I'm just going to bring your attention to the link just above....

Could you post the relevant package/object/relationships please?

http://docs.transfer-orm.com/wiki/Generated_Methods.cfm#OneToMany_Element

What you should be doing is getNAMEOFRELATIONSHIPHERE - does your question actually have any answers related to it?

Stephen


James Allen

unread,
May 11, 2008, 3:22:31 PM5/11/08
to transf...@googlegroups.com

Hi Stephen,

 

I am dealing with the child object though, so getAnswers is not relevant here – I am trying to get to the parent object, which is why I tried getParentQuestion() in the actual answer object.

 

Transfer 1.0 RC1 – Transfer XML:

 

<object name="Question" table="tblQuestions" decorator=" model.questions.question">

                           <id name="ID" type="numeric" />

                            <property name="PublicID" type="string" column="PublicID" />

                           <property name="Question" type="string" column="Question" />

                           <property name="SecondaryInfo" type="string" column="SecondaryInfo" />

                           <property name="Anonymous" type="numeric" column="Anonymous" />

                           <property name="PostDate" type="date" column="PostDate" />

                           <property name="ExpiryDate" type="date" column="ExpiryDate" />

                           <property name="PosterIP" type="string" column="PosterIP" />

                           <property name="LastUpdateDate" type="date" column="LastUpdateDate" />

                           <property name="Rating" type="numeric" column="Rating" />

                           <property name="NoRatings" type="numeric" column="NoRatings" />

                          

                           <!-- Link between a question and the user who posted it -->

                            <manytoone name="User">

                                  <link to="users.Users" column="lnkIDUser"/>

                            </manytoone>

                          

                           <!-- Link between a question and the chosen answer style -->

                            <manytoone name="AnswerStyle">

                                  <link to="question.AnswerStyles" column="lnkIDAnswerStyle"/>

                            </manytoone>

                          

                           <!-- Link between a question and it's images -->

                           <onetomany name="Images">

                                  <link to="question.Image" column="lnkIDQuestion" />

                                 

                                  <collection type="array">

                                         <order property="SortOrder" order="asc" />

                                  </collection>

                           </onetomany>

                                                     

                           <!-- Link between a question and it's answers -->

                           <onetomany name="Answers" lazy="true">

                                  <link to="question.Answer" column="lnkIDQuestion" />

                                 

                                  <collection type="array">

                                         <order property="PostDate" order="asc" />

                                  </collection>

                           </onetomany>

                          

                           <!-- Link between a question and it's answer choices -->

                           <onetomany name="AnswerChoices">

                                  <link to="question.AnswerChoices" column="lnkIDQuestion" />

                                 

                                  <collection type="array">

                                         <order property="ID" order="asc" />

                                  </collection>

                           </onetomany>

                          

                           <!-- Link between the tags and the question -->

                           <manytomany name="Tags" table="lnkQuestion_Tags">

                                  <link to="question.Question" column="lnkIDQuestion"/>

                                  <link to="question.Tag" column="lnkIDTag"/>

 

                                  <collection type="struct">

                                         <key property="Tag" />

                                  </collection>

                           </manytomany>

                     </object>

 

The answer object looks like this:

 

                     <!-- Answer to a question -->

                     <object name="Answer" table="tblAnswers" decorator="model.questions.answers.answer">

                           <id name="ID" type="numeric" />                

                           <property name="AnswerText" type="string" column="AnswerText" />

                           <property name="AnswerOption" type="numeric" column="AnswerOption" />

                           <property name="Rating" type="numeric" column="Rating" />

                           <property name="NoRatings" type="numeric" column="NoRatings" />

                           <property name="OwnerRating" type="numeric" column="OwnerRating" />

                           <property name="PosterIP" type="string" column="PosterIP" />

                           <property name="PostDate" type="date" column="PostDate" />

 

                           <!-- Link between an answer and the user who posted it -->

                            <manytoone name="User">

                                  <link to="users.Users" column="lnkIDUser"/>

                            </manytoone>

                          

                           <!-- Link between an answer and the answer choice if answer choice type -->

                            <manytoone name="AnswerChoice">

                                  <link to="question.AnswerChoices" column="lnkIDAnswerChoice"/>

                            </manytoone>

                                                      

                           <onetomany name="ExtraAnswers">

                                  <link to="question.ExtraAnswer" column="lnkIDParentAnswer" />

                                 

                                  <collection type="array">

                                         <order property="ID" order="asc" />

                                  </collection>

                           </onetomany>

                     </object>

 

The answer I call getParentQuestion() on exists and is associated with a valid question.

 

Thanks,

James.

Mark Mandel

unread,
May 11, 2008, 5:28:37 PM5/11/08
to transf...@googlegroups.com
James


>
>
> The answer I call getParentQuestion() on exists and is associated with a
> valid question.
>

This must not be the case.

The error you are getting is because the Answer you are calling
'getParent' on, doesn't actually HAVE a Parent. It is quite possible
that you have inadvertently grabbed an unpersisted TransferObject from
Transfer, and therefore it doesn't have a parent.

Mark

--
E: mark....@gmail.com
W: www.compoundtheory.com

James Allen

unread,
May 11, 2008, 6:03:08 PM5/11/08
to transf...@googlegroups.com
That'll teach me for working in the sun and not being able to properly see
the screen.. :(

Picked an answer from SQL manager which was actual an 'extra answer' - i.e
an extension of an already posted answer and thus a child of it, so it
didn't have a question ID allocated..

Sorry about that guys.. I had it in my head that I would get problems when
trying to access an object from the child if there wasn't a relationship
defined. Should have checked this out in more detail.. It's been a long
day.. :)

Thanks Mark.

-----Original Message-----
From: transf...@googlegroups.com [mailto:transf...@googlegroups.com]
On Behalf Of Mark Mandel
Sent: 11 May 2008 22:29
To: transf...@googlegroups.com
Subject: [transfer-dev] Re: How to get the parent TO from the child

Brian G

unread,
May 18, 2008, 1:29:57 PM5/18/08
to transfer-dev

A helpful debugging step in these cases is to check with and/or wrap
your calls with a hasParentObject(). I use this a lot in my decorator
validate() routines or in other decorator methods before trying to
access the relationship and has made my code a little more robust (at
least since I use my transfer objects as form beans too).


Brian
Reply all
Reply to author
Forward
0 new messages