-->
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.  [ 7 posts ] 
Author Message
 Post subject: Help! Query problem.
PostPosted: Fri Jun 04, 2004 5:29 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.1.3.
I have a query that I've tested in my DB console and it's working:
Code:
select h1.fornecedor
from historico_es_equip_prod h1
where h1.equip_prod_fk=11 and h1.data_saida = (
               select max(h2.data_saida)
               from historico_es_equip_prod h2
               where h2.equip_prod_fk=h1.equip_prod_fk and h2.data_entrada is null)


My problem is that I can't get it to give me the desired result in HSQL.
Here's my code. I've tried two methods witn no success:

Method 1
Code:
( ... )
List res = session.find("select hep1.fornecedor from vo.HistoricoEsEquipProd hep1 " +
"where hep1.equipProd = ? and hep1.dataSaida = (select max(hep2.dataSaida) from vo.HistoricoEsEquipProd hep2 where hep2.equipProd = hep1.equipProd and hep2.dataEntrada is null)"
,equip_prod_id, Hibernate.LONG);
( ... )


Method 2
Code:
( ... )
List res = session.find("select hep1.fornecedor from vo.HistoricoEsEquipProd hep1 join hep1.equipProd ep1 " +
"where ep1 = ? and hep1.dataSaida = (select max(hep2.dataSaida) from vo.HistoricoEsEquipProd hep2 join hep2.equipProd ep2 where ep2 = ep1 and hep2.dataEntrada is null)"
,equip_prod_id, Hibernate.LONG);
( ... )


NOTE:
None of the above methods give an error. Sintatically there working. The problem is that it's giving me fornecedor = null and in the DB console it's giving !=null. The generated sql code seems correct. Still don't understand why it's not giving me the same results as the native sql code.

--------------------------------------
Generated SQL by Hibernate
--------------------------------------
Code:
select historicoe0_.fornecedor as x0_0_

from historico_es_equip_prod historicoe0_

where (historicoe0_.equip_prod_fk=? )
and(historicoe0_.data_saida=(
            select max(historicoe1_.data_saida)
            from historico_es_equip_prod historicoe1_
            where (historicoe1_.equip_prod_fk=historicoe0_.equip_prod_fk )and(historicoe1_.data_entrada is null )))


-----------------
Mapping file
-----------------
HistoricoEsEquipProd
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.HistoricoEsEquipProd"
    table="historico_es_equip_prod"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="sequence">
           <param name="sequence">historico_es_equip_prod_seq</param>
        </generator>
    </id>

    <property
        name="dataEntrada"
        type="java.sql.Timestamp"
        column="data_entrada"
        length="8"
    />
    <property
        name="dataSaida"
        type="java.sql.Timestamp"
        column="data_saida"
        not-null="true"
        length="8"
    />
    <property
        name="fornecedor"
        type="java.lang.String"
        column="fornecedor"
        not-null="true"
        length="-1"
    />

    <!-- associations -->
    <!-- bi-directional many-to-one association to EquipProd -->
    <many-to-one
        name="equipProd"
        class="vo.EquipProd"
        not-null="true"
    >
        <column name="equip_prod_fk" />
    </many-to-one>

</class>
</hibernate-mapping>



Plese help... don't know what to do.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 5:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
length="-1"
? are you sure of it?

when copy pasting generated sql into a db client what happens?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 5:45 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
anthony wrote:
Quote:
length="-1"
? are you sure of it?

when copy pasting generated sql into a db client what happens?


Thanks for the replay.

In my Postgres console, I executed the generated code:
Code:
select historicoe0_.fornecedor as x0_0_

from historico_es_equip_prod historicoe0_

where (historicoe0_.equip_prod_fk=? )
and(historicoe0_.data_saida=(
            select max(historicoe1_.data_saida)
            from historico_es_equip_prod historicoe1_
            where (historicoe1_.equip_prod_fk=historicoe0_.equip_prod_fk )and(historicoe1_.data_entrada is null )))


and got a surprise:
Code:
ERROR:  operator does not exist: bigint =?
HINT:  No operator matches the given name and argument type(s). You may need to add explicit type casts.


--------------------
Here's my DDL
--------------------
Code:
CREATE TABLE HISTORICO_ES_EQUIP_PROD (
       id INT8 NOT NULL
     , equip_prod_fk INT8 NOT NULL
     , data_entrada TIMESTAMP
     , data_saida TIMESTAMP NOT NULL
     , fornecedor TEXT NOT NULL
     , PRIMARY KEY (id)
     , CONSTRAINT hist_es_equip_prod_fk FOREIGN KEY (equip_prod_fk)
                  REFERENCES EQUIP_PROD (id)
);


How can this be?
Executing via Java I get no HibernateException.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 5:47 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
you have to replace "?" with the data you are working on

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 5:50 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
anthony wrote:
you have to replace "?" with the data you are working on


oops... :)
I replaced the ? and in the console it worked as expected.
So why isn't it working as expected in HQL?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 5:55 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
verify:
- all the type used for this property
- get/set pair
- are you sure it is really null?
- are you sure you're getting first list element and cast it into String?


and what does lengh = -1 mean? i'm not familiar with this mapping attribute.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 04, 2004 6:06 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
anthony wrote:
verify:
- all the type used for this property
- get/set pair
- are you sure it is really null?
- are you sure you're getting first list element and cast it into String?


and what does lengh = -1 mean? i'm not familiar with this mapping attribute.



My mapping files where generated with Middlegen and length=-1 meens unlimited length.
My Value Objects where generated hbm2java.

I found the problem. My stupid mistake of "forgetting" to get the string...


Thanks VERY MUCH for the help.


Can you help on a post that I've made some days ago and still don't have any reply?

http://forum.hibernate.org/viewtopic.php?t=931359


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