2012-09-18 9 views
9

Ho tre tabelle A B e C. Ora voglio eseguire questo query SQL in HQL:Utilizzando sinistra si unisce HQL su 3 tabelle

select * from A as a 
left join 
B as b 
on 
a.id = b.id 
left join 
C as c 
on 
b.type=c.type; 

Hai bisogno di aiuto per iscritto equivalente HQL. Ho provato con questo HQL ...

Query q = session.createQuery(
    "FROM A as a 
    LEFT JOIN 
    B as b 
    on 
    a.id=b.id 
    LEFT JOIN 
    C as c 
    on 
    b.type=c.type"); 

Questa query sta gettando eccezione .....

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: LEFT near line 1, column 23 [FROM com.admin.A as a LEFT JOIN B as b where a.Id=b.Id LEFT JOIN C as c where b.type=c.type] at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

Ho anche provato con "con" e "on" clausole invece di dove ... ottengo lo stesso token imprevisto su "on" o "con"

eccezione qith oN .....

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ON near line 1, column 41 [FROM com.admin.A as a LEFT JOIN B as b on a.Id=b.Id LEFT JOIN C as c onb.type=c.type] at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

ho anche provato con "con" clausola s invece di dove ... ottengo lo stesso token imprevisto o "con"

eccezione qith CON .....

org.hibernate.hql.ast.QuerySyntaxError: unexpected token: ON near line 1, column 41 [FROM com.admin.A as a LEFT JOIN B as b on a.Id=b.Id LEFT JOIN C as c onb.type=c.type] at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:74) at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:214) at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127) at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83) at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:414)

prega di aiuto.

risposta

15

Suppongo che tu abbia già definito tutte le associazioni necessarie nella tua configurazione. Se è così, in HQL sarebbe simile a questa:

from A as a left join a.B as b left join b.C as c 

Non c'è un "ON" dichiarazione HQL, Hibernate non automaticamente in base le mappature e le Associazioni definiti.

Prestare attenzione a a.B e b.C.

Si fa riferimento a B e C in base a alias già definiti a & c.

+0

si scosso upvoted si – kakabali

+0

come potremmo archiviare i dati in una lista in questo caso? – kakabali

+0

Questa dovrebbe essere la risposta accettata :) – tusar

7

utilizzare questa query

from A as a 
left join fetch a.B as b 
left join fetch b.C as c 
+0

ti hai sbalordito – kakabali