-->
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: Problem with query
PostPosted: Tue May 01, 2007 12:57 pm 
Newbie

Joined: Tue May 01, 2007 11:15 am
Posts: 3
Hi all,

I have so strange issues with a query I am trying to execute. First the interesting parts of tzhe classes the query uses:

Code:
@Entity(name = "EnvironmentOrder")
@Table(name = "EnvironmentOrder")
public class Order extends AbstractOrder {
[nothing interesting here]
}


@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class AbstractOrder implements TransactionalObject {
        @ManyToOne
   @JoinColumn(name="environmentId")
   protected Environment environment;

        protected boolean imported = false;

        protected OrderStatus status = OrderStatus.CREATED; //Enumeration
}

@Entity
public class Environment implements TransactionalObject {
      @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   private Order activeOrder;

   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
   private Order incomingOrder;

        @ManyToOne(targetEntity = Zone.class)
   private ZoneValue customer;
}


I think this would be the relevant parts of the classes.
The hql-query goes here:
Code:
select count(o)  from EnvironmentOrder o 
   where  o.status in (:allowedStatusList)
   and  o.environment.customer.id = :customerid
   and  (
      (o.environment.activeOrder is null and o.environment.incomingOrder.imported = 0 )
      or o.environment.activeOrder.id = o.id
      )

First: Are there any obvious errors in this query?

this hql-query results in the following SQL:
Code:
select count(order0_.id) as col_0_0_ from EnvironmentOrder order0_
   inner join AbstractOrder order0_1_ on order0_.id=order0_1_.id,
   Environment environmen1_,
   EnvironmentOrder order4_
   inner join AbstractOrder order4_1_ on order4_.id=order4_1_.id
      where environmen1_.incomingOrder_id=order4_.id
      and order0_1_.environmentId=environmen1_.id
      and (order0_1_.status in (7 , 9 , 10))
      and environmen1_.customer_zone_id=1
      and ((environmen1_.activeOrder_id is null)
      and order4_1_.imported=0 or environmen1_.activeOrder_id=order0_.id)


Which in my case gives the result 0.
In my opinion this generated SQL is wrong and should read:
Code:
select count(order0_.id) as col_0_0_ from EnvironmentOrder order0_
   inner join AbstractOrder order0_1_ on order0_.id=order0_1_.id,
   Environment environmen1_,
   EnvironmentOrder order4_
   inner join AbstractOrder order4_1_ on order4_.id=order4_1_.id
   
      where ( environmen1_.incomingOrder_id=order4_.id OR environmen1_.activeOrder_id=order4_.id )
   
      and order0_1_.environmentId=environmen1_.id
      and (order0_1_.status in (7 , 9 , 10))
      and environmen1_.customer_zone_id=1
      and ((environmen1_.activeOrder_id is null)
      and order4_1_.imported=0 or environmen1_.activeOrder_id=order0_.id)


The last query gives the correct count in my case.

For comparison, I tried the following hql-query which should give less or equal results:
Code:
select count(o)  from EnvironmentOrder o 
   where  o.status in (:allowedStatusList)
   and  o.environment.customer.id = :customerid
   and  o.environment.activeOrder.id = o.id


which evaluated to:
Code:
select count(order0_.id) as col_0_0_ from EnvironmentOrder order0_
   inner join AbstractOrder order0_1_ on order0_.id=order0_1_.id,
   Environment environmen1_
      where order0_1_.environmentId=environmen1_.id
      and (order0_1_.status in (7 , 9 , 10))
      and environmen1_.customer_zone_id=1
      and environmen1_.activeOrder_id=order0_.id

Could anybody explain this behaviour to me? Is there anything wrong with my query or did i discover a bug in hibernate?
This error is reproducible with hibernate 3.1.1 and 3.1.3.
Are there any workarounds for this problem, or would I have to go for two seperate queries for a combined result? I should not, that upgrading to hibernate 3.2.x is no option currently, since it is used in a large project which would require many changes to work with 3.2.x
Would be great if I would receive any help on this.

Thanks in advance,
Philipp

Hibernate version:
3.1.3
Mapping documents:
annotations

Name and version of the database you are using:
Mysql 5


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 6:19 pm 
Newbie

Joined: Tue May 01, 2007 11:15 am
Posts: 3
Ok, my problem seems to be solved. I found out that adding
Code:
left join o.environment.activeOrder as aOrder left join o.environment.incomingOrder as iOrder
to the begnning of the query does the trick.
But I still donĀ“t really now, why it is necessary. Would be nice, if somebody explains that to me.


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.