-->
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.  [ 10 posts ] 
Author Message
 Post subject: Get objects from superclass using a simple query
PostPosted: Wed Jan 11, 2006 3:24 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
Hello all. I´m trying to get some objects in a particular situation. I´ve two mapped classes: Super and Child. Super is the superclass of the Child.

When I´m trying to execute a simple query, trying to get the Super objects, I can only get the Child elements! Why this happens?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 5:33 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you actually mean superclass, then the objects of the child class are of the superclass. Just cast the child objects to the superclass, if that's all you want. If you don't understand the concept, there are a lot of good books out there on object orientation.

If you're misusing the terminology and mean an object and its nested objects, then you need a bidirection association to get from the nested objects to the nesting object. Post your mapping file (using the code tag) here and I'll show you how to add the inverse mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 5:43 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
My mapping:

Code:
<joined-subclass name="ime.sodc.model.local.Local" table="local"
         lazy="false">

         <key column="oid" />

         <property name="descricao" type="java.lang.String"
            update="true" insert="true" access="property" column="descricao" />

         <property name="nome" type="java.lang.String" update="true"
            insert="true" access="property" column="nome" />

<!--
         <many-to-one name="posicaoGeografica"
            class="ime.sodc.model.local.PosicaoGeografica" column="id_posicao"
            cascade="save-update,merge" outer-join="false" lazy="false"/>
-->
         <property name="oidPosicaoGeografica" type="long" />

         <many-to-one name="divisaoTerritorialPai"
            class="ime.sodc.model.local.DivisaoTerritorial"
            column="id_divpai" lazy="false" cascade="save-update,merge" outer-join="false"/>
<!--
         <many-to-one name="pontoDeReferenciaGeografico"
            class="ime.sodc.model.local.Ponto" column="id_ponto"
            cascade="save-update,merge" lazy="false">
         </many-to-one>
-->
         <property name="oidPontoDeReferenciaGeografico" type="long" />
         
         
         <joined-subclass
            name="ime.sodc.model.local.DivisaoTerritorial"
            table="divisaoterritorial" lazy="false">

            <key column="oid" />

            <!-- <property name="tipo" column="tipo"></property>-->

            <many-to-one name="tipo"
               class="ime.sodc.model.local.TipoDivisaoTerritorial"
               column="id_tipodivisaoterritorial"
               cascade="save-update,merge" lazy="false">
            </many-to-one>

            <property name="area" column="area"></property>
            <property name="codigoIBGE" column="codigoibge"></property>
            <property name="sigla" column="sigla"></property>

            <list name="listaSubDivisoes" table="listasubdivisoes"
               cascade="save-update,merge" lazy="false" inverse="false" outer-join="false">
               <key column="id_divpai"></key>
               <index column="count" />
               <one-to-many class="ime.sodc.model.local.Local" />
            </list>

            <joined-subclass name="ime.sodc.model.local.Municipio"
               table="municipio" lazy="false">

               <key column="oid" />
            </joined-subclass>


         </joined-subclass>


Hope that helps to help me hehe

my query is something like this:

Code:
List lista;
         
Query sqlQuery = aSession.createSQLQuery("select oid from divisaoterritorial where codigoibge like '__'")
                              .addScalar("oid",Hibernate.LONG);         
         
List listaValores = sqlQuery.list();

         
Query q = aSession.createQuery("from tipo in class Local where tipo.oid in (:id_list)");
q.setParameterList("id_list",listaValores);
         
lista = q.list();


To perform more efficiency (sorry about my english), i´m getting a list of oids to perform a query in other table. In this example, I have a list of oids from the table divisaoterritorial and I´m using this list to do a query on the table local.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 5:46 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
Just to complete: my list references a list of elements of DivisaoTerritorial and Local (why?). I just want a list of Local.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 5:52 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
"from Local ..." instead of "from tipo" will return Local

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 11, 2006 6:08 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Assuming that field "codigoibge" in TipoDivisaoTerritorial is mapped, then your solution (run an SQL query to get OIDs, then use those OIDs to get Locals that contain Tipos with those OIDs) is significantly less efficient than having hibernate do it all for you.

Eliminate the SQL query, and use this query instead of the one you already have:

Code:
session.createQuery("from Local as l join l.tipo as t where t.codigoibge = '__'");


This will mean that your java app never has to load the list of OIDs, saving not only memory but also all the time required to allocate that memory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 3:08 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
tenwit, following your example, I tried this:

Code:
Query q = aSession.createQuery("from Local as l join l.oid as j where j.codigoIBGE like '__'");


In my situation, I´ve the Class Local that is superclass of DivisaoTerritorial. Then, The oids of Local are the same of the DivisaoTerritorial.

I´m getting this error doing this query:

Code:
java.lang.NullPointerException
   at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:312)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3252)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3044)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2922)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:690)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:546)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:283)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:231)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:214)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:154)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:101)
   at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:473)
   at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1032)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:982)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
   at ime.sodc.bd.testers.TesteQyerySuper.main(TesteQyerySuper.java:23)


What´s wrong? thanks for the help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 5:37 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Maybe j is null. I am not sure, try an exlicit inner join to be sure.

Regards Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 12, 2006 9:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I'm afraid my hibernate source code doesn't match up with yours, so I can't debug your stack trace :( However, HqlSqlWalker is one of the best-commented files in hibernate, so you may be able to figure out what's going on by following the stack trace yourself. Increasing your debug level would help, too.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 5:22 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
I´ve tjis kind of situation:

oid from DivisaoTerritorial references the oid of Local
oid from Local references the oid of Planeta

Any ideas?


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