-->
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.  [ 2 posts ] 
Author Message
 Post subject: HQL with parameters always return empty collection.
PostPosted: Mon Feb 13, 2006 4:20 pm 
Newbie

Joined: Wed Feb 08, 2006 10:02 am
Posts: 2
Location: Fortaleza - CearĂ¡ - Brazil
Hi,

I have this problem:
When I use one or more parameters in WHERE clausule of my HQL the result it's brings me nothing at all (collection not populated).

I already tested the generated statement (with parameters) on my SQL client and returned many rows.

this HQL NOT return rows:
Code:
<query name="app.retrieveByEmpresaAndLoggedUser">
          SELECT   G.theIndustria
          FROM   app.GrupoAcessoUsuario G
          WHERE   G.theEmpresa.codigo = :codEmpresa
          AND      G.usuario = :usuario
</query>


this HQL return rows:
Code:
<query name="app.retrieveByEmpresaAndLoggedUser">
          SELECT   G.theIndustria
          FROM   app.GrupoAcessoUsuario G
</query>


Any one have idea?

Thanks ;)

HQL
Code:
   
<query name="app.retrieveByEmpresaAndLoggedUser">
          SELECT   G.theIndustria
          FROM   app.GrupoAcessoUsuario G
          WHERE   G.theEmpresa.codigo = :codEmpresa
          AND      G.usuario = :usuario
</query>


Hibernate version:
3.1.2

Mapping documents:

Mapping for GrupoAcessoUsuario
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
    <class
        name="app.GrupoAcessoUsuario"
        table="GRP_ACESSO_USU"
    >

        <composite-id
            name="codigo"
            class="app.GrupoAcessoUsuarioPK"
        >
                   
                    <key-many-to-one
                        name="theIndustriaEmpresa"
                        class="app.IndustriaEmpresa">
                        <column name="COD_EMPRESA"></column>
                        <column name="COD_INDUSTRIA"></column>
                    </key-many-to-one>

                    <key-many-to-one
                        name="theGrupoAcesso"
                        class="app.GrupoAcesso"
                        column="COD_GRP_ACESSO"/>

                    <key-property
                        name="usuario"
                        type="java.lang.String"
                        column="LOGIN_USU" />

        </composite-id>
       
        <property
           name="tipoOperacao"
         type="java.lang.String"
           column="COD_GRP_OPER"
           insert="true"
           update="true" />

        <property
           name="usuario"
         type="java.lang.String"
           column="LOGIN_USU"
           insert="false"
           update="false" />
           
        <many-to-one
            name="theIndustria"
            class="app.Industria"
            cascade="none"
            outer-join="auto"
            update="false"
            insert="false"
            column="COD_INDUSTRIA"
           />
       
        <many-to-one
            name="theEmpresa"
            class="app.Empresa"
            cascade="none"
            outer-join="auto"
            update="false"
            insert="false"
            column="COD_EMPRESA"
           />

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-OrgVendaCliente.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>


</hibernate-mapping>

Mapping for Industria (HBM for named query and annotation for mapping)

Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 1.1//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   
    <!-- 
       
       Query para retornar as industrias de uma empresa e de um usuario logado
          
    -->
    <query name="app.retrieveByEmpresaAndLoggedUser">
          SELECT   G.theIndustria
          FROM   app.GrupoAcessoUsuario G
          WHERE   G.theEmpresa.codigo = :codEmpresa
          AND      G.usuario = :usuario
    </query>
   
</hibernate-mapping>


Class with annotations
Code:
package app;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import app.BaseEntity;

@Entity
@Table(name = "INDUSTRIA")
@javax.persistence.SequenceGenerator(name = "SEQ_INDUSTRIA", sequenceName = "SEQ_INDUSTRIA")
public class Industria extends BaseEntity {

   private static final long serialVersionUID = 989245700212868771L;

   private java.lang.Integer codigo;

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_INDUSTRIA")
   @Column(name = "COD_INDUSTRIA")
   public java.lang.Integer getCodigo() {
      return this.codigo;
   }

   public void setCodigo(java.lang.Integer newValue) {
      this.codigo = newValue;
   }

   private java.lang.String descricao;

   @Column(name = "DEN_INDUSTRIA")
   public java.lang.String getDescricao() {
      return this.descricao;
   }

   public void setDescricao(java.lang.String newValue) {
      this.descricao = newValue;
   }

   private java.lang.Integer sequencialSimulado;

   @Column(name = "ULT_COD_SIM")
   public java.lang.Integer getSequencialSimulado() {
      return this.sequencialSimulado;
   }

   public void setSequencialSimulado(java.lang.Integer newValue) {
      this.sequencialSimulado = newValue;
   }

}


The generated SQL (show_sql=true):

Hibernate: select industria1_.COD_INDUSTRIA as COD1_15_, industria1_.DEN_INDUSTRIA as DEN2_15_, industria1_.ULT_COD_SIM as ULT3_15_ from GRP_ACESSO_USU grupoacess0_, INDUSTRIA industria1_ where grupoacess0_.COD_INDUSTRIA=industria1_.COD_INDUSTRIA and grupoacess0_.COD_EMPRESA=? and grupoacess0_.LOGIN_USU=?

real statement with parameters
select industria1_.COD_INDUSTRIA as COD1_15_, industria1_.DEN_INDUSTRIA as DEN2_15_, industria1_.ULT_COD_SIM as ULT3_15_ from GRP_ACESSO_USU grupoacess0_, INDUSTRIA industria1_ where grupoacess0_.COD_INDUSTRIA=industria1_.COD_INDUSTRIA and grupoacess0_.COD_EMPRESA='1' and grupoacess0_.LOGIN_USU='1'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 13, 2006 6:27 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Try making the query as explicit as possible. You can reinstate the implicit join once you get the query going. Start with this:
Code:
SELECT   G.theIndustria
FROM   app.GrupoAcessoUsuario G
JOIN G.theEmpresa e
WHERE   e.codigo = :codEmpresa
AND      G.usuario = :usuario
Also note that in HQL, you use class names not table names: is app.GrupoAcessoUsuario a class name? It looks like a table name..


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