论坛首页 入门技术论坛

HQL子查询怎么会出错呢??

浏览 11416 次
该帖已经被评为新手帖
作者 正文
   发表时间:2006-11-20  
大家好!
现在做项目的时候,碰到了一个问题,请帮忙看看怎么会事吧,谢谢了先~~
1)映射文件:

<hibernate-mapping >
    <class name="Employee" table="EMPLOYEE">
       <composite-id name="Id" class="EmployeePK">
<key-property name="BusinessUnitId" column="BUSINESS_UNIT_ID" type="string" length="2" />
<key-property name="EmployeeId" column="EMPLOYEE_ID" type="string" length="10"/>
    </composite-id>
   <property
      name="FirstName"
      column="FIRST_NAME"
      type="string"
      length="40"
      not-null="true"
   />
   <property
      name="LastName"
      column="LAST_NAME"
      type="string"
      length="40"
      not-null="true"
   />
</class>
</hibernate-mapping>

2)代码:
  String s = "select x.FirstName,x.LastName from (select x.FirstName,x.LastName from Employee x)";
  List list = this.getSession().createQuery(s).list();

  当然了,那个子查询里面还有n多条件,为了方便大家找原因,我把条件去掉了。即使加上那些条件,还是一样的错误。
3)错误信息:

2006-11-20 19:52:09,100 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] - parse() - HQL:  select x.FirstName,x.LastName from(  select x.FirstName,x.LastName from Employee x)
2006-11-20 19:52:09,100 ERROR [org.hibernate.hql.PARSER] - line 1:36: unexpected token: (
2006-11-20 19:52:09,100 DEBUG [org.hibernate.hql.ast.ErrorCounter] - line 1:36: unexpected token: (
line 1:36: unexpected token: (
at org.hibernate.hql.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1584)
at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1408)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:248)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.pei.webpdm.itma.dao.hibernate.LabelImpl.getDivisionLabelList(LabelImpl.java:60)
at com.pei.webpdm.itma.dao.TestILabelDao.testgetDivisionLabelList(TestILabelDao.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
2006-11-20 19:52:09,100 ERROR [org.hibernate.hql.PARSER] - line 1:69: unexpected token: from
2006-11-20 19:52:09,100 DEBUG [org.hibernate.hql.ast.ErrorCounter] - line 1:69: unexpected token: from
line 1:69: unexpected token: from
at org.hibernate.hql.antlr.HqlBaseParser.fromClassOrOuterQueryPath(HqlBaseParser.java:1909)
at org.hibernate.hql.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1564)
at org.hibernate.hql.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1428)
at org.hibernate.hql.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1130)
at org.hibernate.hql.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:702)
at org.hibernate.hql.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:296)
at org.hibernate.hql.antlr.HqlBaseParser.statement(HqlBaseParser.java:159)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:248)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:157)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.pei.webpdm.itma.dao.hibernate.LabelImpl.getDivisionLabelList(LabelImpl.java:60)
at com.pei.webpdm.itma.dao.TestILabelDao.testgetDivisionLabelList(TestILabelDao.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
2006-11-20 19:52:09,116 DEBUG [org.hibernate.hql.ast.AST] - --- HQL AST ---
\-[QUERY] 'query'
    \-[SELECT_FROM] 'SELECT_FROM'
       +-[FROM] 'from'
       \-[SELECT] 'select'
          +-[DOT] '.'
          |  +-[IDENT] 'x'
          |  \-[IDENT] 'FirstName'
          \-[DOT] '.'
             +-[IDENT] 'x'
             \-[IDENT] 'LastName'
   发表时间:2006-11-20  
没见过那么怪的查询语句!没有查询条件吗?
0 请登录后投票
   发表时间:2006-11-20  

SQL文写错了
从简单的东西试起
0 请登录后投票
   发表时间:2006-11-20  
daoger 写道
没见过那么怪的查询语句!没有查询条件吗?

呵呵,不是没有查询条件,只是为了方便,将相关的条件隐去了。
情况是这样的,这是一个子查询的一部分

select x.FirstName,x.LastName from Employee x
实际上后面是一个group by分组查询,在结果中取到FirstName之后,再让FirstName作为令外的条件再进行查询。举个例子,比如查询一个部门内姓李的员工的考勤信息。
所有我需要select x.FirstName from(Select x.FirstName,x.LastName from Employee x group by x.FirstName,x.LastName having ...(略去)) where 各种条件。

我把所有的条件都去掉,发现这里select x.FirstName,x.LastName from(开始出错,所以我只贴了这部分的代码。

PS:非常感谢你的关注,如果发现什么问题了,情及时回复哦。谢谢了先。
0 请登录后投票
   发表时间:2006-11-20  
抛出异常的爱 写道

SQL文写错了

写错了,问题是不知道错在那里了。如果知道的话,提醒一声吧。
这个已经是最简单的东西了吧?实在不知道再从那里开始试起。

自己的感觉应该是前半部门有问题,可是如果从一个查询结果中取一个子集,那个sql文该如何写呢?
0 请登录后投票
   发表时间:2006-11-20  
引用
select x.FirstName,x.LastName from (select x.FirstName,x.LastName from Employee x)


在上面的查询语句中x的有效范围应该是
引用
(select x.FirstName,x.LastName from Employee x)

在括号之外,x应该是无效的!
0 请登录后投票
   发表时间:2006-11-21  
那个的确是不正确的,我分别试了x.*;*;firstName、lastName;FirstName、LastName;....几乎可能的值都试过了,可是还是不对呢。
真不知道类似这样的HQL该怎么写了。
0 请登录后投票
   发表时间:2006-11-21  
知道了,是Hibernate本身不支持在from后的子查询的,只支持where子句后的子查询。
这下可比较郁闷了...
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics