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 1Code:
( ... )
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 2Code:
( ... )
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
-----------------
HistoricoEsEquipProdCode:
<?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.