-->
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.  [ 5 posts ] 
Author Message
 Post subject: select only parts of related child table/collection
PostPosted: Fri Apr 09, 2004 9:48 am 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
Hi!

I have a 2 tables: Questions and Answers. They are mapped in a one-many relationship. In the Questions class I therefor have a Set that will hold all answers.

What I am trying to do, is to select a few fields from Question,and a few fields from Answer, since I do not need all of them.

Now, I try something like:

"Select question.title,question.answers.title from my.answers as answer"

It seems I cannot speficically choose anything that has to do with the collection / related child table. As soon as I try to select speficic fields , it complains that it wants "indices".

So the question is, how can this be done?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 09, 2004 9:50 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
select question.title, answer.title from Question question join question.answer as answer


Top
 Profile  
 
 Post subject: not the desired result
PostPosted: Fri Apr 09, 2004 1:07 pm 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
That did not give me the desired result I wanted. The statement actually performs a join, which I am not interested in.
The bean classes I've created does not seem to like it.

I want hibernate to act as it usually does when it comes to mapping: select from parent table and select from Child table in two different operations:
Select field1,field2,field3....etc. from Questions where id=something
Select field1,field2,field3....etc from answers where id=someQuestionID

The only thing I want different, is to control the fields selected from the child table, since I do not need all of them all the time.
Select field1,field2,field3....etc. from Questions where id=something
Select field1,field2 from answers where id=someQuestionID

There must be some way to do this?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 11:46 am 
Newbie

Joined: Tue Apr 06, 2004 6:37 am
Posts: 17
Location: Oslo, Norway
I'm working together with treklipper on this and we have now tried the following:

Quote:
Query query = session.createQuery("select t.tittel from net.esimulation.db.innboksovelse.InnboksOppgave t");


I could then iterate through the results the following way:
Code:
List l = query.list();
java.util.Iterator i = l.iterator();

while(i.hasNext()) {
  System.out.println(i.next());
}


So far so good..

When trying to get two columns from the table, the result is wrapped in an object. But what kind of object is this? When casting to the object it is mapped to, I get a ClassCastException.

Code:
Query query = session.createQuery("select t.tittel,t.innboksOppgaveID from net.esimulation.db.innboksovelse.InnboksOppgave t");

List l = query.list();
java.util.Iterator i = l.iterator();

while(i.hasNext()) {
   InnboksOppgave o = (InnboksOppgave)i.next(); // Exception HERE!
   System.out.println(o.getinnboksOppgaveID() + " " + o.gettittel());
}


So what kind of object is this? Is it not possible to cast without having all the columns?

Are we missing anything in our mapping file?
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping >
    <class name="net.esimulation.db.innboksovelse.InnboksOppgave" table="innboksOppgaver" >
      <id name="innboksOppgaveID" column="innboksOppgaveID" type="int" unsaved-value="0">
         <generator class="identity"/>
      </id>
       <property name="innboksOvelseID" column="innboksOvelseID" type="int" />
       <property name="tittel" column="tittel" type="java.lang.String" />
       <property name="hovedtekst" column="hovedtekst" type="java.lang.String" />
       <property name="fraPerson" column="fraPerson" type="java.lang.String" />
       <property name="oppgaveDato" column="oppgaveDato" type="java.util.Date" />
       <property name="anbefaltTekst" column="anbefaltTekst" type="java.lang.String" />
       <property name="anbefaltGrupperingID" column="anbefaltGrupperingID" type="int" />
       <property name="anbefaltHurtighet" column="anbefaltHurtighet" type="int" />
       <property name="anbefaltViktighet" column="anbefaltViktighet" type="int" />
    </class>
</hibernate-mapping>
[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 11, 2004 2:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
It is an Object[2] as documented in http://www.hibernate.org/hib_docs/reference/html/query-language.html#query-language-s4


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