-->
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.  [ 2 posts ] 
Author Message
 Post subject: unnecessary join in subquery
PostPosted: Thu Jun 25, 2009 11:38 am 
Newbie

Joined: Thu Jun 25, 2009 10:22 am
Posts: 5
unnecessary join in subquery

Hi,

for the following hql query hibernate generates a sql statement with an unnecessary join in the subselect

Code:
select count(distinct pnode.productid) from ProductNode pnode where pnode.id in (select attribute.productNode from Attribute attribute where attribute.name = ? and attribute.val = ?)


Results in following SQL
Code:
select count(distinct productnod1_.productid) from product_node productnod1_ where productnod1_.id in (select attribute2_.product_node_id from attribute attribute2_, product_node productnod3_ where attribute2_.product_node_id=productnod3_.id and attribute2_.name='Bauart' and attribute2_.val='Barebone-PC')

The subquery on the Attribute table joins unnecessary the product_node table.

The code in grails for my domain classes:
Code:
class ProductNode {
  List<Attribute> attrs
}

class Attribute {
  String name
  String val
  static belongsTo = [productNode: ProductNode]
}


I have shorten the domain class code and queries to hopefully relevant parts of my problem. In truth I dynammically append more subselects to get all productnodes that matches an attribute list and productnode is in truth a tree like structure...

Is there a trick to tell hibernate not to use the unnecessary join?
Can someone confirm, that this happens in a pure java (non grails) environment too?

Best regards,
pddd


Top
 Profile  
 
 Post subject: Re: unnecessary join in subquery
PostPosted: Thu Jul 02, 2009 9:14 am 
Newbie

Joined: Thu Jun 25, 2009 10:22 am
Posts: 5
Ok, I ve found the solution myself.

Code:
class Attribute {
  String name
  String val
  static belongsTo = [productNode: ProductNode]

  static fetchMode = [productNode:"lazy"]

  static mapping = {
    columns {
          productNode lazy:true
      }
  }

}


If productNode is lazy, hibernate does not generate the unnecessary join.

Bye
pddd


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