-->
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.  [ 4 posts ] 
Author Message
 Post subject: unexpected behaviour of EJB3 QL left join
PostPosted: Thu Feb 23, 2006 9:27 am 
Newbie

Joined: Tue Apr 26, 2005 10:45 am
Posts: 7
Hello Hibernate Community,

I am using JBoss EJB3 RC5 with PostgreSQL 8.1 and
I would like to make an EJB3 QL query that looks like this:
Code:
SELECT DISTINCT s FROM ServiceCase s LEFT JOIN s.products AS p WHERE ( s.id > 0) OR (p.productType.name LIKE '%xxxx%')


From my point of view this query should list all instances of class ServiceCase, because the expression
Code:
(s.id > 0)
is always true. Instead it lists only the instances of class ServiceCase that are associated with at least one instance of class Product ... so i am missing all these service cases that have no products. This means, although the where-clause evaluates to true, the select statement produces no results if there is no instance of class ServiceCase with at least one product in it's product list.

Do i use the LEFT JOIN the right way or is this possibly a bug?

A code listing of the classes i use:
Code:

@Entity
public class ServiceCase implements Serializable {
   private int id;   
   private Set<Product> products = new HashSet<Product>();

   @Id(generate=GeneratorType.AUTO)
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
   public Set<Product> getProducts() {
      return products;
   }

   public void setProducts(Set<Product> products) {
      this.products = products;
   }
}


@Entity
public class Product implements Serializable {

   private int id;

   private ProductType productType;

   @Id(generate=GeneratorType.AUTO)
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }
   
   @ManyToOne(optional=false)
   public ProductType getProductType() {
      return productType;
   }

   public void setProductType(ProductType product) {
      this.productType = product;
   }
}


@Entity
public class ProductType implements Serializable {

   private String id;
   private String name;

   @Id
   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

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


Best regards,
Alexander


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 4:48 am 
Newbie

Joined: Tue Jan 10, 2006 11:59 am
Posts: 17
Alexander,

I think you should turn on "show sql" option and check the result sql.

If there is left join, then you need to check your database.
If there is inner join, then it is a bug in hibernate, and you need to report it on JIRA hibernate bugtrack.

_________________
--
Sergey Vladimirov


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 27, 2006 6:55 am 
Newbie

Joined: Tue Apr 26, 2005 10:45 am
Posts: 7
Thank you Sergey. I think the query was wrong. I had to add an additional left join clause. To work as expected, the query must be:
Code:
SELECT DISTINCT s FROM ServiceCase s LEFT JOIN s.products AS p LEFT JOIN p.productType AS t WHERE ( s.id > 0) OR (t.name LIKE '%xxxx%')


Best regards,
Alexander


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 01, 2006 11:18 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
EJB-QL like SQL is ternary logic, not binary logic

_________________
Emmanuel


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