两张表、一张用户表(Tbsysuserinfo)、一张问题表(Tbquestion)
问题表中有用户编号外键hbm映射如下:<many-to-one name="username" class="com.boxun.crm.dao.entities.Tbsysuserinfo" fetch="select" lazy="false">
            <column name="USERNAME" precision="22" scale="0">   
              <comment>客户经理:为当前登录的用户</comment>  
            </column>           
        </many-to-one> javaBean:private Tbsysuserinfo username = new Tbsysuserinfo(); 
public Tbsysuserinfo getUsername() {
return username;
} public void setUsername(Tbsysuserinfo username) {
this.username = username;
}在未配置该映射之前、程序运行正常。
开始我以为数据没有添加关系、这样映射会有问题、但是我映射另一个对象时证明是没有问题的!
不过在添加这个映射后、查询Hql就出错了!public List<Tbquestion> queryTbquestion(String wheres,String whereName,PageInfo pageInfo,Object areaId,String department) throws Exception{
String hql = " from com.boxun.crm.dao.entities.Tbquestion t where 1=1  and t.rid like '0851%'  order by t.newtime desc " ;
String hqlCount = " select count(*) " + hql;  
//设值给totalCount
pageInfo.setTotalCount(daoHql.listCount(hqlCount));   
//设值totalPage
pageInfo.setTotalPage((pageInfo.getTotalCount()+ 10 - 1) / 10);  
hql = " select * "+hql+ " order by t.newtime desc " ; //按时间倒序排列        
return daoHql.find(hql, 10, pageInfo.getCurPage());

}在daoHql.listCount(hqlCount);的时候没有错误、
错误出现在daoHql.find()这里!
错误信息如下:09:13:00,062 INFO  [STDOUT] ERROR [http-0.0.0.0-8080-4]                                  MAIL     - 192.168.0.1bin>>queryTbquestionqueryTbquestionunexpected token: * near line 1, column 9 [ select *  from com.boxun.crm.dao.entities.Tbquestion t where 1=1  and t.rid like '0851%'  order by t.newtime desc ]
09:13:00,062 ERROR [STDERR] org.hibernate.hql.ast.QuerySyntaxException: unexpected token: * near line 1, column 9 [ select *  from com.boxun.crm.dao.entities.Tbquestion t where 1=1  and t.rid like '0851%'  order by t.newtime desc ]
09:13:00,062 ERROR [STDERR]  at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
09:13:00,062 ERROR [STDERR]  at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
09:13:00,062 ERROR [STDERR]  at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
09:13:00,062 ERROR [STDERR]  at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
09:13:00,062 ERROR [STDERR]  at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
09:13:00,062 ERROR [STDERR]  at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
09:13:00,062 ERROR [STDERR]  at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
09:13:00,062 ERROR [STDERR]  at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
09:13:00,062 ERROR [STDERR]  at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
09:13:00,062 ERROR [STDERR]  at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
09:13:00,062 ERROR [STDERR]  at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
09:13:00,062 ERROR [STDERR]  at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1586)
09:13:00,062 ERROR [STDERR]  at com.boxun.crm.dao.impl.Dao_HqlImp.find(Dao_HqlImp.java:148)
09:13:00,062 ERROR [STDERR]  at com.boxun.crm.service.proptype.impl.QuestionImpl.queryTbquestion(QuestionImpl.java:181)我把hql拷贝到PLSQL里面去执行的时候、有可以查询出值来!!!
这是个什么情况???

解决方案 »

  1.   


    String hql = " from com.boxun.crm.dao.entities.Tbquestion t where 1=1  and t.rid like '0851%'这个hql语句其实最原始不是这样写的!后面的时间排序也没有!
    是我大意了。
      因为我这边有很多条件才到控制台copy了一下!
    原句是这样的:    from Tbquestion t where 1=1 到后面才有select *  加上时间降序排列的!
      

  2.   

    hql = " select t "+hql+ " order by t.newtime desc " ; //按时间倒序排列 
      

  3.   

    没注意看你打印的Hql语句,打印的语句如下:
    select *  from com.boxun.crm.dao.entities.Tbquestion t where 1=1  and t.rid like '0851%'  order by t.newtime desc所以可以把 select * 去掉

    hql = hql+ " order by t.newtime desc " ; //按时间倒序排列 
      

  4.   

    用t的时候生成的hql语句是这个样子的:Hibernate: select * from ( select tbquestion0_.QID as QID46_, tbquestion0_.ECID as ECID46_, tbquestion0_.ECNAME as ECNAME46_, tbquestion0_.CLIENTNAME as CLIENTNAME46_, tbquestion0_.PHONE as PHONE46_, tbquestion0_.TITLE as TITLE46_, tbquestion0_.INFOMSG as INFOMSG46_, tbquestion0_.QUESTIONSTA as QUESTION8_46_, tbquestion0_.RESERVETIME as RESERVET9_46_, tbquestion0_.OVERRESTIME as OVERRES10_46_, tbquestion0_.OVERREMARK as OVERREMARK46_, tbquestion0_.FILEURL as FILEURL46_, tbquestion0_.NEWTIME as NEWTIME46_, tbquestion0_.UPTIME as UPTIME46_, tbquestion0_.UPPEOPLE as UPPEOPLE46_, tbquestion0_.USERNAME as USERNAME46_, tbquestion0_.DEPARTMENT as DEPARTMENT46_, tbquestion0_.STATUS as STATUS46_, tbquestion0_.RETURNNAME as RETURNNAME46_, tbquestion0_.RETURNSUBMIT as RETURNS20_46_, tbquestion0_.RETURNINQUIRY as RETURNI21_46_, tbquestion0_.RETURNTIME as RETURNTIME46_, tbquestion0_.RETURNTIME2 as RETURNTIME23_46_, tbquestion0_.APPRAISE as APPRAISE46_, tbquestion0_.IDEA as IDEA46_, tbquestion0_.MAINNAME as MAINNAME46_, tbquestion0_.ATIME as ATIME46_, tbquestion0_.CALLID as CALLID46_, tbquestion0_.RID as RID46_, tbquestion0_.TEMP1 as TEMP30_46_, tbquestion0_.TEMP2 as TEMP31_46_, tbquestion0_.TEMP3 as TEMP32_46_ from GZYD.TBQUESTION tbquestion0_ where 1=1 and (tbquestion0_.RID like '0851%') order by tbquestion0_.NEWTIME desc ) where rownum <= ?这样的话、异常是:09:35:54,750 INFO  [STDOUT] ERROR [http-0.0.0.0-8080-7]                                  MAIL     - 192.168.0.1bin>>queryTbquestionqueryTbquestioncould not execute query
    09:35:54,765 ERROR [STDERR] org.hibernate.exception.GenericJDBCException: could not execute query
    09:35:54,765 ERROR [STDERR]  at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.loader.Loader.doList(Loader.java:2235)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.loader.Loader.list(Loader.java:2124)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:411)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1084)
    09:35:54,765 ERROR [STDERR]  at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    09:35:54,765 ERROR [STDERR]  at com.boxun.crm.dao.impl.Dao_HqlImp.find(Dao_HqlImp.java:156)
    09:35:54,765 ERROR [STDERR]  at com.boxun.crm.service.proptype.impl.QuestionImpl.queryTbquestion(QuestionImpl.java:181)
      

  5.   

    直接这样的话、hql语句会跟加上T一样的错误!
    不知道是分页还是什么情况、Hibernate总会给在原来的sql基础上加上 select * from () where rownum <= ?
      

  6.   

    QuerySyntaxException: unexpected token:
     肯定是hql语句有问题。
      

  7.   

    不知道是分页还是什么情况、直接from查询Hibernate会自动给我加上
    select * from () where rownum <= ? 
    这样的话就会出现跟加上 select T 一样的错误! 
      

  8.   

    Hql 语句我直接去掉实体路径到PLSQL执行是可以的!
    还有以前没有加上配置的时候、运行也是没有问题!
    就是不知道现在这是个什么情况!
      

  9.   

    再次确认一个信息就是配置没有问题、因为我在其他地方也有用到这个映射的对象!
      在页面上直接username.getUserName();这样在页面能取到值!
    所以映射是没有问题的!
       应该就是在Hql这边出了问题了!
      

  10.   

    public List<Object> find(String hql, int row, int pages)
    throws HibernateException {
    if (null == hql || hql.equals("")) {
    return null;
    }
    query = this.getSession().createQuery(hql);
    if (row > 0 && pages > 0) {
    // 取得每页显示结果集
    query.setMaxResults(row);
    // 取得当前页码所要显示的结果集
    query.setFirstResult(row * (pages - 1));
    } return query.list(); }
      

  11.   

    如果我直接使用原生态Sql的话、这样也是没有问题的!
        但是这样我页面用起来有点麻烦!
      

  12.   

    把分页改成这样:
    query.setFirstResult(row * (pages - 1)).setMaxResults(row)然后select * 去掉看看打印的hql是什么样子
      

  13.   


    hql + " order by t.newtime desc " ;if (row > 0 && pages > 0) {
    query.setFirstResult(row * (pages - 1)).setMaxResults(row);
    // 取得每页显示结果集
    //query.setMaxResults(row);       
    // 取得当前页码所要显示的结果集
    //query.setFirstResult(row * (pages - 1));
    }Hibernate: select * from ( select tbquestion0_.QID as QID46_, tbquestion0_.ECID as ECID46_, tbquestion0_.ECNAME as ECNAME46_, tbquestion0_.CLIENTNAME as CLIENTNAME46_, tbquestion0_.PHONE as PHONE46_, tbquestion0_.TITLE as TITLE46_, tbquestion0_.INFOMSG as INFOMSG46_, tbquestion0_.QUESTIONSTA as QUESTION8_46_, tbquestion0_.RESERVETIME as RESERVET9_46_, tbquestion0_.OVERRESTIME as OVERRES10_46_, tbquestion0_.OVERREMARK as OVERREMARK46_, tbquestion0_.FILEURL as FILEURL46_, tbquestion0_.NEWTIME as NEWTIME46_, tbquestion0_.UPTIME as UPTIME46_, tbquestion0_.UPPEOPLE as UPPEOPLE46_, tbquestion0_.USERNAME as USERNAME46_, tbquestion0_.DEPARTMENT as DEPARTMENT46_, tbquestion0_.STATUS as STATUS46_, tbquestion0_.RETURNNAME as RETURNNAME46_, tbquestion0_.RETURNSUBMIT as RETURNS20_46_, tbquestion0_.RETURNINQUIRY as RETURNI21_46_, tbquestion0_.RETURNTIME as RETURNTIME46_, tbquestion0_.RETURNTIME2 as RETURNTIME23_46_, tbquestion0_.APPRAISE as APPRAISE46_, tbquestion0_.IDEA as IDEA46_, tbquestion0_.MAINNAME as MAINNAME46_, tbquestion0_.ATIME as ATIME46_, tbquestion0_.CALLID as CALLID46_, tbquestion0_.RID as RID46_, tbquestion0_.TEMP1 as TEMP30_46_, tbquestion0_.TEMP2 as TEMP31_46_, tbquestion0_.TEMP3 as TEMP32_46_ from GZYD.TBQUESTION tbquestion0_ where 1=1 and (tbquestion0_.RID like '0851%') order by tbquestion0_.NEWTIME desc ) where rownum <= ?还是一样的~~~!!!
    10:17:17,468 INFO  [STDOUT] ERROR [http-0.0.0.0-8080-3]                                  MAIL     - 192.168.0.1bin>>queryTbquestionqueryTbquestioncould not execute query
    10:17:17,468 ERROR [STDERR] org.hibernate.exception.GenericJDBCException: could not execute query
      

  14.   

    春哥说你还是换成SQL吧。select * from(select xxx from xxx)..  hibernate不支持这种子查询吧。为什么你的hql语句生成后会这样的呢?
      

  15.   

    我记得hql查询实体类,是不需要用select,直接是from 、、、、、吧
    where 1=1 and (tbquestion0_.RID like '0851%') 这生成的sql有问题吧,应该无法执行吧
      

  16.   


    11:01:53,953 INFO  [STDOUT] Hibernate: select count(tbquestion0_.CID) as col_0_0_ from GZYD.TBQUESTIONCHANGE tbquestion0_, GZYD.TBQUESTION tbquestion1_ where tbquestion1_.QID=tbquestion0_.QID and (tbquestion1_.RETURNTIME is null) and (tbquestion1_.RETURNTIME2 is null) and (tbquestion0_.RID like '0851%')
    11:01:53,953 INFO  [STDOUT] Hibernate: select * from ( select tbquestion0_.CID as col_0_0_, tbquestion1_.TITLE as col_1_0_, tbquestion1_.RESERVETIME as col_2_0_, tbquestion0_.CHANGEOPERATE as col_3_0_ from GZYD.TBQUESTIONCHANGE tbquestion0_, GZYD.TBQUESTION tbquestion1_ where tbquestion1_.QID=tbquestion0_.QID and (tbquestion1_.RETURNTIME is null) and (tbquestion1_.RETURNTIME2 is null) and (tbquestion0_.RID like '0851%') order by tbquestion0_.CREATETIME desc ) where rownum <= ?
    11:01:53,968 INFO  [STDOUT] INFO  [http-0.0.0.0-8080-7]                          OperationTxt     - 192.168.0.1bin>>queryTbquestionChange返回[Ljava.lang.Object;@1b04652这是加上Hibernate的分页过后、他生成的Hql语句!
    是可以的、上面的这个hql就可以!
    事实证明这样是可以的!
    所以...........
    我很纠结、查询另外一个实体的时候没有问题!
    这个实体我只是加上了这么一个配置就完蛋了!
    以前运行也是可以的!
      

  17.   


    我的错、耽误大家宝贵的时间!
    抱歉了:
    问题是这样的~~~!!!
    关联主键的字段是Long类型!Oracle数据中用Number表示!
    而外键在数据库则是用Varchar2类型表示!
    Ok、结贴!
      

  18.   

    怎么我的主键生成的都是BigDecimal类型
      

  19.   

    自己更改一下就好了!!!
    换成Long或者Integer的!