【问题标题】:Error in my hibernate request我的休眠请求出错
【发布时间】:2010-08-02 10:19:58
【问题描述】:

您好,我有这个 HQL 请求,但在运行时出现错误

我的功能:

public ProprietaireInt getProprietaireInt(String codeImmeuble, String annee) {
   //Create connexion
   Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
   ProprietaireInt proprietaireInt = new ProprietaireInt();
   try{
         String query = "SELECT pi.siren,pi.codeSCI,pi.libSocieteInterne,pi.libSocieteNormalise,pi.codeDomainePilotage " +
                        "FROM ImmeubleFisc if" +
                        "JOIN proprietaireInt pi ON if.sirenProprietaire = pi.siren " +
                        "WHERE if.idImmeubleFisc.codeImmeuble = :codeImmeuble " +
                        "AND if.idImmeubleFisc.exerciceFiscal = :annee";
        Query q = null;
        q = session.createQuery(query);
        q.setString("codeImmeuble", codeImmeuble);
        q.setString("annee", annee);

        proprietaireInt = (ProprietaireInt) q.uniqueResult();
        session.flush();
    }
    catch (Exception e)
    {
       System.out.println(e.getMessage()+" "+e.getStackTrace());
    }
    return  proprietaireInt;
}

我的错误:

2010-08-02 12:12:22,081 错误 ast.ErrorCounter (ErrorCounter.java:33) - 第 1:156 行: 意外令牌:properetaireInt

【问题讨论】:

    标签: java hibernate hql orm


    【解决方案1】:

    我不确定是否支持 JOIN ON(请参阅 HHH-16HHH-620)但是,假设 ProprietaireInt 是一个实体,我猜 JOIN 子句应该是(注意大写首字母):

    JOIN ProprietaireInt pi ...
    

    此外,由于您使用的是投影(没有构造函数表达式),因此您将得到 Object[] 作为结果,我认为以下内容不会起作用:

    proprietaireInt = (ProprietaireInt) q.uniqueResult();
    

    来自参考文档:

    14.6. The select clause

    ...

    查询可以返回多个对象 和/或属性作为类型数组 Object[]:

    select mother, offspr, mate.name
    from DomesticCat as mother
        inner join mother.mate as mate
        left outer join mother.kittens as offspr
    

    或作为List

    select new list(mother, offspr, mate.name)
    from DomesticCat as mother
        inner join mother.mate as mate
        left outer join mother.kittens as offspr
    

    或者 - 假设类 Family 有一个适当的构造函数 - 作为 实际的类型安全 Java 对象:

    select new Family(mother, mate, offspr)
    from DomesticCat as mother
        join mother.mate as mate
        left join mother.kittens as offspr
    

    另见

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      相关资源
      最近更新 更多