-->
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.  [ 8 posts ] 
Author Message
 Post subject: many-to-one problem
PostPosted: Fri Nov 21, 2003 11:03 am 
Newbie

Joined: Fri Nov 21, 2003 10:58 am
Posts: 4
Every Answer has a Question
Question knows all the Answers he has

If I retreive an Answer out of a HQL I can get the String attributes, but I can't get the Question object
answer.getStringAnswer() gives me the answer
answer.getQuestion() gives me NULL

Answer.java has the attributes
private Question question;
private int intAnswer;
private String stringAnswer;
private boolean booleanAnswer;


Question.java has the attributes
private List answers;
private String description;
private String answerType;
private boolean required;

Question.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
   <class name="com.xxess.platform.domain.Question" table="question">
      
      <id name="id">
         <generator class="native" />
      </id>
      
      <property name="description" type="java.lang.String"/>
      <property name="answerType" type="java.lang.String" />
      <property name="required" type="java.lang.Boolean" />

         <bag name="answers" lazy="true" inverse="true" cascade="all">
            <key column="question"/>
            <one-to-many class="com.xxess.platform.domain.Answer" />
        </bag>
       
   </class>
</hibernate-mapping>


Answer.hbm.xml:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
   <class name="com.xxess.platform.domain.Answer" table="Answer">
      
      <id name="id">
         <generator class="native" />
      </id>
      
      <property name="intAnswer" type="java.lang.Integer"/>
      <property name="stringAnswer" type="java.lang.String" />
      <property name="booleanAnswer" type="java.lang.Boolean" />

        <many-to-one name="question" not-null="false" />

   </class>
</hibernate-mapping>


I tried about everything, but nothing worked :{
What am I doing wrong here ?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 11:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
broken get/set pair?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 11:33 am 
Newbie

Joined: Fri Nov 21, 2003 10:58 am
Posts: 4
what do you mean with broken ?

in the database I can see that the Answer actually has a Question
and every attribute has a public getter and setter in the code

by the way, the query to fetch the Answer is:
Quote:
select distinct new Answer(answer.id, answer.intAnswer, answer.stringAnswer, answer.booleanAnswer)
from Answer answer
where ....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 11:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well no wonder!!

What on earth is wrong with

Code:
from Answer where...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 11:48 am 
Newbie

Joined: Fri Nov 21, 2003 10:58 am
Posts: 4
the retreivel of the Answer is done correctly

Code:
select distinct new Answer(answer.id, answer.intAnswer, answer.stringAnswer, answer.booleanAnswer)
from Answer answer
where anser.id = '1234'

gives me an asnwer...

Code:
if(iterator.hasNext())
   answer = (Answer)iterator.next();
System.out.println(answer.getStringAnswer());

shows the String the answer contains (wich is correct)
while answer.getQuestion() retruns NULL


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 21, 2003 12:30 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Does it seem reasonable to you that stringAnswer is properly initilized because it appears in the select clause, whereas question is not, because it does not?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 6:47 am 
Newbie

Joined: Fri Nov 21, 2003 10:58 am
Posts: 4
Stupid me !

the sollution indeed was:
Code:
select distinct new Answer(answer.id, answer.intAnswer, answer.stringAnswer, answer.booleanAnswer, question)
from Answer answer, Question question
where answer.id = '1234'


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 22, 2003 8:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh ? Why are you using the new Answer(x,y,z) construct instead of just doing "from Answer where id = 42" ?

Do you want the complete object or just a partial content ?

If complete: then use "from Answer ..."
If partial: then use the "select new Answer(...) from Answer"

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.