2010-07-27 70 views
0

你好,我有一個錯誤,當我執行與休眠的請求錯誤在我的休眠請求

ERROR ast.ErrorCounter(ErrorCounter.java:33) - 行1:22:期待IDENT,發現 '*'

吾道:

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; 
    } 

我的hbm.xml文件:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
<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> 

回答

1

嘗試在你的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; 
} 

如果不指定字段,則不需要SELECT。

+0

工作,但現在我有一個像 FE_TARIF_IDF錯誤沒有被映射 – Mercer 2010-07-27 12:56:05

+0

這意味着該領域「FE_TARIF_IDF」是不是在你的Hibernate映射。 請務必檢查它是否大寫(例如fe_tarif_idf!= FE_TARIF_IDF) – 2010-07-27 13:35:14

+0

這也可能是因爲Guillaume的迴應如下:您在查詢中使用表名和列名。 Hibernate需要類名和變量名(來自這些類)。 – 2010-07-27 14:01:21

0

select ...子句在HQL可選的,你可以去from Table where...

或使用一個別名:select o from Table o where ...

此外,在HQL表名稱應與類名和列被替換屬性:

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