-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problems with subqueries.
PostPosted: Fri Feb 13, 2004 11:11 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.1.1.
My mapping files where generated with Middlegen R3.
Can't get my query to work. Keeps giving me:

Code:
net.sf.hibernate.QueryException: path expression ends in a composite value: versaodocu0_.id [from vo.VersaoDocumento vd1 where vd1.comp_id.documento.id = ? and vd1.id = (select max(vd2.id) from vo.VersaoDocumento vd2 where vd2.comp_id.documento.id = ?)]
   at net.sf.hibernate.hql.PathExpressionParser.getWhereColumn(PathExpressionParser.java:370)
   at net.sf.hibernate.hql.WhereParser.doPathExpression(WhereParser.java:352)
   at net.sf.hibernate.hql.WhereParser.doToken(WhereParser.java:366)
   at net.sf.hibernate.hql.WhereParser.token(WhereParser.java:251)
   at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
   at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
   at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
   at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
( ... )


What I want is the max VersaoDocumento for a given Documento.

---------
Code
---------

Code:
( ... )
session = sessionFactory.openSession();
         
List res = session.find("from vo.VersaoDocumento vd1 where vd1.comp_id.documento.id = ? and " +
         "vd1.id = (select max(vd2.id) from vo.VersaoDocumento vd2 where vd2.comp_id.documento.id = ?)",
         new Object[] {documento_id, documento_id}, new Type[] {Hibernate.LONG, Hibernate.LONG});
( ... )      


------------------
Mapping files
------------------

VersaoDocumento
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>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.VersaoDocumento"
    table="versao_documento"
>

    <composite-id name="comp_id" class="vo.VersaoDocumentoPK">
        <key-property
            name="id"
            column="id"
            type="long"
            length="8"
        />
        <!-- bi-directional many-to-one association to Documento -->
        <key-many-to-one
           name="documento"
           class="vo.Documento"
       >
           <column name="documento_fk" />
       </key-many-to-one>
    </composite-id>   

    <property
        name="descricao"
        type="java.lang.String"
        column="descricao"
        length="-1"
    />

    <!-- associations -->

</class>
</hibernate-mapping>


Documento
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>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.Documento"
    table="documento"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="nome"
        type="java.lang.String"
        column="nome"
        length="-1"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to VersaoDocumento -->
    <set
        name="versaoDocumentos"
        lazy="true"
        inverse="true"
        cascade="delete"
    >
        <key>
            <column name="documento_fk" />
        </key>
        <one-to-many
            class="vo.VersaoDocumento"
        />
    </set>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:06 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The id property is a "special" property name. Please see the documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:15 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
The id property is a "special" property name. Please see the documentation.


Thanks for the reply Gavin.
I found the problem.

It should have been:
Code:
( ... )
session = sessionFactory.openSession();
         
List res = session.find("from vo.VersaoDocumento vd1 where vd1.comp_id.documento.id = ? and " +
         "vd1.comp_id.id = (select max(vd2.id) from vo.VersaoDocumento vd2 where vd2.comp_id.documento.id = ?)",
         new Object[] {documento_id, documento_id}, new Type[] {Hibernate.LONG, Hibernate.LONG});
( ... )


I had vd1.id instead of having vd1.comp_id.id.
It's working now.


But I have another problem regarding these mapping files.
I have the following query:

Code:
( ... )
List res = session.find("from vo.Documento d join fetch d.versaoDocumentos vd where d.id = ? order by vd.comp_id.id asc", documento_id, Hibernate.LONG);
( ... )


The query works but it ignores the order by clause!?
Can't I do an order by like this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Show the SQL.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:28 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
Show the SQL.


NOTE: in the generated SQL code there will appear columns that don't exist in the posted mapping files. I took them out on purpose to simplify the reading.

Here's the generated SQL code by Hibernate:

Code:
Hibernate: select documento0_.id as id0_, versaodocu1_.id as id1_, versaodocu1_.documento_fk as document2_1_, documento0_.nome as nome0_, documento0_.descricao as descricao0_, documento0_.user_id as user_id0_, documento0_.dt_hr as dt_hr0_, documento0_.pasta_documento_fk as pasta_do6_0_, versaodocu1_.descricao as descricao1_, versaodocu1_.user_submissao as user_sub4_1_, versaodocu1_.data_submissao as data_sub5_1_, versaodocu1_.aprovado as aprovado1_, versaodocu1_.user_aprovacao as user_apr7_1_, versaodocu1_.data_aprovacao as data_apr8_1_, versaodocu1_.user_id as user_id1_, versaodocu1_.dt_hr as dt_hr1_, versaodocu1_.id as id__, versaodocu1_.documento_fk as document2___ from documento documento0_ inner join versao_documento versaodocu1_ on documento0_.id=versaodocu1_.documento_fk where (documento0_.id=? ) order by  versaodocu1_.id asc

Hibernate: select documento0_.id as id3_, documento0_.nome as nome3_, documento0_.descricao as descricao3_, documento0_.user_id as user_id3_, documento0_.dt_hr as dt_hr3_, documento0_.pasta_documento_fk as pasta_do6_3_, docproxver1_.documento_fk as document1_0_, docproxver1_.versao_actual as versao_a2_0_, docproxver1_.user_id as user_id0_, docproxver1_.dt_hr as dt_hr0_, pastasdocu2_.id as id1_, pastasdocu2_.nome as nome1_, pastasdocu2_.descricao as descricao1_, pastasdocu2_.user_id as user_id1_, pastasdocu2_.dt_hr as dt_hr1_, pastasdocu2_.pasta_documento_fk as pasta_do6_1_, pastasdocu3_.id as id2_, pastasdocu3_.nome as nome2_, pastasdocu3_.descricao as descricao2_, pastasdocu3_.user_id as user_id2_, pastasdocu3_.dt_hr as dt_hr2_, pastasdocu3_.pasta_documento_fk as pasta_do6_2_ from documento documento0_ left outer join doc_prox_versao docproxver1_ on documento0_.id=docproxver1_.documento_fk left outer join pastas_documento pastasdocu2_ on documento0_.pasta_documento_fk=pastasdocu2_.id left outer join pastas_documento pastasdocu3_ on pastasdocu2_.pasta_documento_fk=pastasdocu3_.id where documento0_.id=?


Can't I order a collection for every returned Documento?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, you just posted two SQL queries, one of which has an order by clause!

Whats the problem?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:38 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
Well, you just posted two SQL queries, one of which has an order by clause!

Whats the problem?



I'm printing my results, and I have:

Code:
----- DOCUMENTO -----
ID: 3 + NOME: xpto.pdf
------ VERSAO_DOCUMENTO ------
ID: 2 ++ DESC: null ++ DATE: 2004-02-12 16:46:24.046
ID: 1 ++ DESC: null ++ DATE: 2004-02-12 16:43:05.0
------------------------------------


In the VERSAO_DOCUMENTO section the ID is not ordered. That's my problem.
The order by vd.comp_id.id asc clause in my query is being ignored...
Don't know what's the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I find this incredibly difficult to believe.

You are mistaken.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 13, 2004 12:48 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gavin wrote:
I find this incredibly difficult to believe.

You are mistaken.


I've just gave you all the information that I have.
Really don't know what's the problem.

Maybe using an "session.filter" on the collection afterwards will do the trick.
What do you think?


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