-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: LEFT JOIN for not associated classes
PostPosted: Mon Dec 04, 2006 12:22 pm 
Newbie

Joined: Mon Dec 04, 2006 10:48 am
Posts: 3
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.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 12:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this syntax is not legal
it's should be something like
from Document doc LEFT JOIN doc.item item WITH ...

If there is no association between doc and item, then only a theta style inner join is possible, no outer join

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Thanks, I confirm my suspect
PostPosted: Thu Dec 07, 2006 4:48 pm 
Newbie

Joined: Mon Dec 04, 2006 10:48 am
Posts: 3
If there no association between classes, then is imposible to do left outer join,

this rule, is not explicit in the reference documents, neither in the api documents.


Greetings.

PD. somebody could implements LEFT OUTER JOINS between classes not
associated.


Top
 Profile  
 
 Post subject: Re: Thanks, I confirm my suspect
PostPosted: Sun Dec 17, 2006 4:38 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
sergioszy wrote:
PD. somebody could implements LEFT OUTER JOINS between classes not
associated.


How about you :-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: RE: How about you :-)
PostPosted: Mon Dec 18, 2006 4:01 pm 
Newbie

Joined: Mon Dec 04, 2006 10:48 am
Posts: 3
OK,

but give me some clue, where to start with.

Greetings.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 18, 2006 9:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
it is related to the entity / collection Loaders and the AST parser, but I'm not familiar with this area. This is not a trivial task.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.