0

我们正在尝试为下面创建休眠特定查询 lSession.createQuery(lQueryString.toString()); ............

INSERT INTO EMPLOYEE_HISTORY (emp_status,
                              createdBy,
                              releasedate,
                              year)
   SELECT CASE n.emp_Status WHEN 0 THEN 'Draft' ELSE 'Final' END,
          m.createdBy,
          m.releasedate,
          m.year
     FROM EMPLOYEE m, (SELECT COUNT (1) EMPLOYEE_HISTORY   b,EMPLOYEE a where a.emplid=b.emplid and a.year=b.year and b.year=2012 and a.doc_id='XYZ') n 
     where a.doc_id='xyz'

getting following exception,I guess it because of below statement in query
(SELECT COUNT (1) EMPLOYEE_HISTORY   b,EMPLOYEE a where a.emplid=b.emplid and a.year=b.year and b.year=2012 and a.doc_id='XYZ') n


org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 374 
INSERT INTO EMPLOYEE_HISTORY (emp_status,
                              createdBy,
                              releasedate,
                              year)
   SELECT CASE n.emp_Status WHEN 0 THEN 'Draft' ELSE 'Final' END,
          m.createdBy,
          m.releasedate,
          m.year
     FROM EMPLOYEE m, (SELECT COUNT (1) EMPLOYEE_HISTORY   b,EMPLOYEE a where a.emplid=b.emplid and a.year=b.year and b.year=2012 and a.doc_id='XYZ') n 
     where a.doc_id='xyz'

例外

at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
    at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:124)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1770)

如果有任何建议/意见,请提供。

4

1 回答 1

1

看起来,就像您将 SQL 而不是 HQL 传递给 Session.createQuery(...) 方法。

见:http ://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html#objectstate-querying-executing

例如在:

INSERT INTO EMPLOYEE_HISTORY (...)

'EMPLOYEE_HISTORY' 应该是实体类名(如 EmployeeHistory)而不是表名。

同样在 HQL 中,您可以使用在实体中定义的引用来通过关联避免通过 id 进行隐式连接,就像您在此处所做的那样

where a.emplid=b.emplid and ...

考虑上面 URL 中的示例:

select mother from Cat as cat join cat.mother as mother where cat.name = ?
于 2013-03-13T10:48:35.450 回答