Hi,
I have two classes not associated, Item and Document, and I need to generate
a projection with a OUTER JOIN,
SELECT doc.number, item.description, item.code FROM Document doc LEFT JOIN org.jboss.tutorial.entity.bean.Item item WITH doc.number = item.document
Example:
package org.jboss.tutorial.entity.bean;
import javax.persistence.Table;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.Id;
@Entity
@Table(name="docs")
public class Document {
@Id
@Column(name="num")
private int number;
@Column(name="descrp")
private String description;
public int getNumber() { return number; }
public String getDescription() { return description; }
public void setNumber( int xx) { this.number = xx;}
public void setDescription(String xx) { this.description = xx;}
}
package org.jboss.tutorial.entity.bean;
import javax.persistence.Table;
import javax.persistence.Entity;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.IdClass;
@Entity
@IdClass(ItemId.class)
@Table(name="item")
public class Item {
@Id
@Column(name="docs_num")
private int document;
@Id
@Column(name="code")
private String code;
@Column(name="descrp")
private String description;
public int getDocument() { return document; }
public String getCode() { return code; }
public String getDescription() { return description; }
public void setDocument( int xx) { this.document = xx; }
public void setCode(String xx) { this.code = xx;}
public void setDescription(String xx) { this.description = xx;}
}
I need to generate the next sql
SELECT docs.num, item.code, item.descrp
FROM docs, OUTER item WHERE docs.num = item.docs_num
I try with the JPQL
manager.createQuery(
"SELECT doc.number, item.description, item.code FROM Document doc LEFT JOIN org.jboss.tutorial.entity.bean.Item item WITH doc.number = item.document").
getResultList();
But I have the Exception:
Caused by: java.lang.NullPointerException
at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:320)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3275)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3067)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2945)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
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 org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:92)
at org.jboss.ejb3.entity.TransactionScopedEntityManager.createQuery(TransactionScopedEntityManager.java:127)
at org.jboss.tutorial.entity.bean.VerRubrosBean.ver(VerRubrosBean.java:47)
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 org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.ExtendedPersistenceContextPropagationInterceptor.invoke(ExtendedPersistenceContextPropagationInterceptor.java:57)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:197)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateful.StatefulInstanceInterceptor.invoke(StatefulInstanceInterceptor.java:83)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:78)
at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:131)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
at org.jboss.ejb3.stateful.StatefulContainer.dynamicInvoke(StatefulContainer.java:319)
at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invoke(AOPRemotingInvocationHandler.java:82)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:828)
at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:681)
at org.jboss.remoting.transport.socket.ServerThread.processInvocation(ServerThread.java:358)
at org.jboss.remoting.transport.socket.ServerThread.dorun(ServerThread.java:412)
at org.jboss.remoting.transport.socket.ServerThread.run(ServerThread.java:239)
And in server.log ...
2006-12-04 08:12:15,357 DEBUG [org.hibernate.impl.SessionImpl] opened session at timestamp: 4772785092022272
2006-12-04 08:12:15,357 DEBUG [org.hibernate.jdbc.JDBCContext] TransactionFactory reported no active transaction; Synchronization not registered
2006-12-04 08:12:15,357 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] Looking for a JTA transaction to join
2006-12-04 08:12:15,357 DEBUG [org.hibernate.jdbc.JDBCContext] successfully registered Synchronization
2006-12-04 08:12:15,357 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] Looking for a JTA transaction to join
2006-12-04 08:12:15,357 DEBUG [org.hibernate.ejb.AbstractEntityManagerImpl] Transaction already joined
2006-12-04 08:12:15,358 DEBUG [org.hibernate.hql.ast.QueryTranslatorImpl] parse() - HQL: SELECT doc.number, item.description, item.code FROM org.jboss.tutorial.entity.bean.Document doc LEFT JOIN org.jboss.tutorial.entity.bean.Item item WITH doc.number = item.document
2006-12-04 08:12:15,364 DEBUG [org.hibernate.hql.ast.AST] --- HQL AST ---
\-[QUERY] 'query'
\-[SELECT_FROM] 'SELECT_FROM'
+-[FROM] 'FROM'
| +-[RANGE] 'RANGE'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[DOT] '.'
| | | | | | | +-[IDENT] 'org'
| | | | | | | \-[IDENT] 'jboss'
| | | | | | \-[IDENT] 'tutorial'
| | | | | \-[IDENT] 'entity'
| | | | \-[IDENT] 'bean'
| | | \-[IDENT] 'Document'
| | \-[ALIAS] 'doc'
| \-[JOIN] 'JOIN'
| +-[LEFT] 'LEFT'
| +-[DOT] '.'
| | +-[DOT] '.'
| | | +-[DOT] '.'
| | | | +-[DOT] '.'
| | | | | +-[DOT] '.'
| | | | | | +-[IDENT] 'org'
| | | | | | \-[IDENT] 'jboss'
| | | | | \-[IDENT] 'tutorial'
| | | | \-[IDENT] 'entity'
| | | \-[IDENT] 'bean'
| | \-[IDENT] 'Item'
| +-[ALIAS] 'item'
| \-[WITH] 'WITH'
| \-[EQ] '='
| +-[DOT] '.'
| | +-[IDENT] 'doc'
| | \-[IDENT] 'number'
| \-[DOT] '.'
| +-[IDENT] 'item'
| \-[IDENT] 'document'
\-[SELECT] 'SELECT'
+-[DOT] '.'
| +-[IDENT] 'doc'
| \-[IDENT] 'number'
+-[DOT] '.'
| +-[IDENT] 'item'
| \-[IDENT] 'description'
\-[DOT] '.'
+-[IDENT] 'item'
\-[IDENT] 'code'
2006-12-04 08:12:15,364 DEBUG [org.hibernate.hql.ast.ErrorCounter] throwQueryException() : no errors
2006-12-04 08:12:15,364 DEBUG [org.hibernate.hql.antlr.HqlSqlBaseWalker] select << begin [level=1, statement=select]
2006-12-04 08:12:15,365 DEBUG [org.hibernate.hql.ast.tree.FromElement] FromClause{level=1} : org.jboss.tutorial.entity.bean.Document (doc) -> document0_
2006-12-04 08:12:15,367 DEBUG [org.hibernate.hql.ast.util.LiteralProcessor] setSQLValue() org.jboss.tutorial.entity.bean.Item -> null
2006-12-04 08:12:15,367 DEBUG [org.hibernate.hql.ast.tree.FromReferenceNode] Resolved : org.jboss.tutorial.entity.bean.Item -> null
2006-12-04 08:12:15,367 DEBUG [org.hibernate.jdbc.JDBCContext] TransactionFactory reported no active transaction; Synchronization not registered
2006-12-04 08:12:15,367 DEBUG [org.hibernate.jdbc.JDBCContext] TransactionFactory reported no active transaction; Synchronization not registered
Greetings.
|