2013-03-13 3 views
0

Nous essayons de créer hiberner requête spécifique ci-dessous lSession.createQuery(lQueryString.toString()); ............requêtes imbriquées Hibernate

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' 

L'exception

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) 

S'il vous plaît fournir si toute suggestion/entrée.

Répondre

1

Il semble, comme vous passez SQL à la place de HQL à la méthode Session.createQuery (...).

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

Par exemple, dans:

INSERT INTO EMPLOYEE_HISTORY (...) 

'EMPLOYEE_HISTORY' entité devrait être le nom de la classe (comme EmployeeHistory) pas le nom de la table.

également dans HQL vous pouvez utiliser des références définies dans vos entités à passer par des associations en évitant implicitement rejoindre par ids, comme vous le faites ici

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

exemple d'URL Considérez ci-dessus:

select mother from Cat as cat join cat.mother as mother where cat.name = ?