views:

29

answers:

2

hello i have an error when i execute a request with hibernate

ERROR ast.ErrorCounter (ErrorCounter.java:33) - line 1:22: expecting IDENT, found '*'

my dao:

public List rechercheValeurTarifs() throws Exception {
        List tarifs = null;
        try{

            tarifs = getHibernateTemplate().find("SELECT  FE_TARIF_IDF.* " +
                    "FROM FE_TARIF_IDF " +
                    "WHERE FE_TARIF_IDF.D_ANNEE in (SELECT MAX (FE_EXERCICE_FISCAL.D_CEXER) FROM FE_EXERCICE_FISCAL);");

            if(tarifs == null)
            {
                tarifs = getHibernateTemplate().find("SELECT  FE_TARIF_IDF.* " +
                        "FROM FE_TARIF_IDF " +
                        "WHERE FE_TARIF_IDF.D_ANNEE in (SELECT MAX (FE_EXERCICE_FISCAL.D_CEXER-1) FROM FE_EXERCICE_FISCAL);");
            }
        } catch (Exception e){
        System.out.println(e.getStackTrace());
        }
        return tarifs;
    }

my hbm.xml file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
<hibernate-mapping package="xx.xxx.xxx.persistance.bo" >
    <class name="Tarifs" table="FE_TARIF_IDF" >
            <cache usage="read-write" />
            <composite-id name="idTarif" class="IdTarif">
                <key-property name="annee" type="string" length="4" >
                    <column name="D_ANNE" />
                </key-property>
                <key-property name="zone" type="int" length="1" >
                    <column name="D_NZONE" />
                </key-property>
                <key-property name="typeLocal" type="string" length="1" >
                    <column name="D_CTYPE_LOCAL" />
                </key-property>
            </composite-id>
            <property name="tarifNormal" type="float" column="D_NTARIF_NORMAL" length="7"/>
            <property name="tarifReduit" type="float" column="D_NTARIF_REDUIT" length="7"/>
            <property name="surfaceMin" type="float" column="D_NSURFACEMIN" length="10"/>    
    </class>
</hibernate-mapping>
A: 

The select ... clause is optional in HQL, you can just go with from Table where...

Or use an alias: select o from Table o where ...

Also, in HQL table names should be replaced with class names and column with attributes:

tarifs = getHibernateTemplate().find("FROM Tarifs " +
                    "WHERE idTarif.annee in (SELECT MAX (XXX) FROM ExerciceFiscal)");
Guillaume
+1  A: 

Hello,

try the following in your dao:

public List rechercheValeurTarifs() throws Exception {
    List tarifs = null;
    try{

        tarifs = getHibernateTemplate().find("FROM FE_TARIF_IDF " +
                "WHERE FE_TARIF_IDF.D_ANNEE in (SELECT MAX (FE_EXERCICE_FISCAL.D_CEXER) FROM FE_EXERCICE_FISCAL);");

        if(tarifs == null)
        {
            tarifs = getHibernateTemplate().find("FROM FE_TARIF_IDF " +
                    "WHERE FE_TARIF_IDF.D_ANNEE in (SELECT MAX (FE_EXERCICE_FISCAL.D_CEXER-1) FROM FE_EXERCICE_FISCAL);");
        }
    } catch (Exception e){
    System.out.println(e.getStackTrace());
    }
    return tarifs;
}

You don't need the SELECT if you don't specify fields.

Bart Vangeneugden
work, but now i have an error like FE_TARIF_IDF is not mapped
Mercer
This means the field "FE_TARIF_IDF" is not in your hibernate mapping.Be sure to check if it's uppercase everywhere (e.g. fe_tarif_idf != FE_TARIF_IDF)
Bart Vangeneugden
This could also be because of Guillaume's response below: You're using table names and column names in your query. Hibernate expects class names and variable names (from those classes).
Bart Vangeneugden