-->
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.  [ 13 posts ] 
Author Message
 Post subject: problem in outer join fetching
PostPosted: Fri Dec 14, 2007 4:54 am 
Newbie

Joined: Tue Aug 28, 2007 3:18 am
Posts: 13
Hi ,

I have 3 tables. LKUP , TKT_QST_ACTN and TKT.
TKT_QST_ACTN have foreign key reference of LKUP through LKUP_ID.
and TKT_QST_ACTN have also foreign key reference of TKT through TKT_NO

Now I have to retrieve all the tktQstActn objects for a particular ticket number by joining lkup on lkup.typ.
It is possible that TKT_QST_ACTN dont have all the reference of LKUP_ID for a particular TKT_NO.
But I have to retrieve all the LKUP_ID also for that TKT_NO.

my HQL IS ::

from TktQstActn tqs right join tqs.lkup lk where lk.typ in
('X' , 'Y')


It retrieves object array with first element of tktQstActn and second element as lkup. It retrieves null tktQstActn with valid lkup reference.

but when I want to fetch tktQstQctn for a particular ticket with following query it RETRIEVES ONLY that rows that have a reference with lkup .

HQL is :
from TktQstActn tqs right join tqs.lkup lk where lk.typ in
('X' , 'Y') and tqs.tkt.tktNo = '5'


I am using Eclipse 's hibernate tool to see my result.

In SQL :

SELECT tqa.TKT_NO, tqa.QST_ID, tqa.QST_ACTN, l.CD, l.LKUP_DESC
FROM TKT_QST_ACTN tqa, lkup l
where l.TYP in ('X', 'Y')
and l.LKUP_ID = tqa.QST_ID (+)
and tqa.TKT_NO (+) = 5

solved my purpose

But I dont know how it should be in HQL ?

Can Anyone please throw some light

on how can i retrieve all the tktQstActn objects having reference + not having reference with lkup object for a paarticular ticket number .....


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 5:45 pm 
Newbie

Joined: Fri Dec 28, 2007 5:42 pm
Posts: 6
Hi,I am having the same requirement and I couldn't get HQL to work with outerjoin in the above scenario. Did you find a way out on it? or is it an HQL limitation?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 8:11 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
That is to be expected. The moment you have a condition on an outer join element in the where it will exclude all NULL elements because they don't satisfy the condition. That is standard SQL NULL value semantics. The SQL you provided is a non standard compliant SQL Server / Sybase variant I believe. Try something like this instead:

Code:
from TktQstActn tqs right join tqs.lkup lk where lk.typ in
('X' , 'Y') and (tqs.tkt.tktNo = '5' or tqs.tkt is null)


I haven't checked if HQL supports 'is null' or if not what the equivalent in HQL is.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 1:49 am 
Newbie

Joined: Fri Dec 28, 2007 5:42 pm
Posts: 6
Thanks for your reply.

No. Unfortunately HQL doesn't support "tqs.tks is null".

This is very common scenario, Neither criteria nor HQL supports this scenario. I m tempted to use Native SQL for this :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 1:53 am 
Newbie

Joined: Fri Dec 28, 2007 5:42 pm
Posts: 6
btw, I am using Oracle as backend.

my situation is

EMP table
empId number,
managerId number (Self-FK on EMP table)

but managerId is empty for some empIds. I am trying to fetch 'manager name' if managerId exists for an employee otherwise show empty.

This is so basic situation, I couldn't get it to work using Hibernate.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 4:14 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
According to the Hibernate documentation 'is null' is supported in HQL. See http://www.hibernate.org/hib_docs/v3/re ... xpressions.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 4:26 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
For Employee / Manager Name issue would the following work for you?

Code:
select e, (select m.name from Emp m where m.empId = e.managerId) as managerName from Emp e


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 1:41 pm 
Newbie

Joined: Fri Dec 28, 2007 5:42 pm
Posts: 6
Thanks for your reply.

Unfortunately it doesn't work, because if 'm' (manager doesn't exist for an emp record) then m object is NULL and hibnerate throws 'null pointer exception' because of m.empId.

Yes, HQL supports 'is null' but only at the 'field' level not the 'object level', that means yo cannot say (ex: where e.m is null) , you can only say e.m.managerId is null, but since 'm' itself is null hibernate throws nullpointer exception.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 8:07 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Could you post the actual HQL query causing you grief please.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 2:01 am 
Newbie

Joined: Fri Dec 28, 2007 5:42 pm
Posts: 6
Code:
ORACLE Table
=========

LICENSEE_ENTITY
   entity_id
   state_entity_id      (SELF reference to LICENSEE_ENTITY table)
   entityDescriptiveName

state_entity_id is FK to same LICENSEE_ENTITY table (both entity and state_entity records in the same table)

HQL:  (I want to get the liceseeName and its state name if state exists otherwise null)
====================

select
       licenseeEntity.entityDescriptiveName as licenseeName,
       refStateEntity.entityDescriptiveName as licenseeState
from
     LicenseeEntityDomain as licenseeEntity
     left outer join licenseeEntity.refStateEntity as refStateEntity


Hibernate converts the above HQL like this:
============================

select
  licenseeen0_.ENTITY_DESCRIPTIVE_NAME as col_0_0_,
  bsreflicen1_.ENTITY_DESCRIPTIVE_NAME as col_1_0_
from
  REF_LICENSEE_ENTITY licenseeen0_
left outer join
  REF_LICENSEE_ENTITY bsreflicen1_
   on licenseeen0_.ENTITY_SYSID=bsreflicen1_.REF_STATE_ENTITY_ID

last line is wrong:

instead it should be -
on licenseeen0_.REF_STATE_ENTITY_SYSID=bsreflicen1_.ENTITY_ID



Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 3:31 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Does the following work?
Code:
select
       licenseeEntity.entityDescriptiveName as licenseeName,
       (select refStateEntity.entityDescriptiveName from LicenseeEntityDomain as refStateEntity where refStateEntity.entityId = licenseeEntity.stateEntityId) as licenseeState
from
     LicenseeEntityDomain as licenseeEntity


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 3:52 am 
Newbie

Joined: Fri Dec 28, 2007 5:42 pm
Posts: 6
nope. it wouldn't work, and in-fact I tried this.

Code:

select
       licenseeEntity.entityDescriptiveName as licenseeName,
       (select refStateEntity.entityDescriptiveName from LicenseeEntityDomain as refStateEntity where refStateEntity.entityId = licenseeEntity.stateEntityId) as licenseeState
from
     LicenseeEntityDomain as licenseeEntity

-- in this licenseeEntity.stateEntityId ==> is an Object (as it is self-FKed) so the clause "refStateEntity.entityId = licenseeEntity.stateEntityId" is not valid as one side it is field-name other side it is object.

-- and if u try refStateEntity.entityId = licenseeEntity.stateEntityId.entityId, then hibernate throws 'null-pointer-exception' as stateEntityId (object) would be null in some cases.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 30, 2007 5:20 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Actually it works for me in a similar example I butchered quickly from something I had. Here we have Nodes optionally pointing to Categories which are just other Nodes. So its very similar to your scenario. Here is my entity definition. Its all JPA annotations:

Code:
package au.com.lawlex.taipan.app.core.node;

import java.util.Collection;
import java.util.Collections;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "node_tree_new")
public class NodeLightNew
  implements java.io.Serializable
{

  private long nodeId;
  private Long categoryId;
  private NodeLightNew category;
  private Collection<NodeLightNew> subCategories = Collections.emptyList();
  private String name;

  public NodeLightNew()
  {
  }

  @Column(name = "node_id", unique = true, nullable = false)
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Id
  public long getNodeId()
  {
    return nodeId;
  }

  public void setNodeId(final long nodeId)
  {
    this.nodeId = nodeId;
  }

  @Column(name = "category_id", insertable = false, updatable = false)
  public Long getCategoryId()
  {
    return this.categoryId;
  }

  public void setCategoryId(final Long categoryId)
  {
    this.categoryId = categoryId;
  }

  @ManyToOne(optional = true)
  @JoinColumn(name = "category_id", nullable = true)
  public NodeLightNew getCategory()
  {
    return this.category;
  }

  public void setCategory(final NodeLightNew category)
  {
    this.category = category;
  }

  @OneToMany(targetEntity=au.com.lawlex.taipan.app.core.node.NodeLightNew.class, mappedBy = "category")
  public Collection<NodeLightNew> getSubCategories()
  {
    return this.subCategories;
  }

  public void setSubCategories(final Collection<NodeLightNew> subCategories)
  {
    this.subCategories = subCategories;
  }

  @Column(name = "name", nullable = false)
  public String getName()
  {
    return this.name;
  }

  public void setName(final String name)
  {
    this.name = name;
  }

} // end class NodeLight


And here is the working query:
Code:
select nl1.name, (select nl2.name from NodeLightNew nl2 where nl1.categoryId = nl2.nodeId) from NodeLightNew nl1


And actually this query appears to be working fine as well which is very much like your query:
Code:
select nl1.nodeId, nl1.name, nl2.name from NodeLightNew nl1 left outer join nl1.category nl2


So my feeling now is that may be you haven't got your mappings quite right?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.