-->
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: Possible bug in HQL?
PostPosted: Wed Feb 17, 2010 9:45 am 
Newbie

Joined: Tue Feb 16, 2010 5:10 pm
Posts: 1
Greetings everyone,

Since I don't know how to describe the problem in a concise way, I will instead post code snippets to reproduce the problem.

Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class SubDocument implements Serializable {
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @AccessType(value = "field")
   private Long id;
   
   public Long getID() {
      return id;
   }
}

@Entity
public class Order extends SubDocument {
   @Temporal(TemporalType.DATE)
    @AccessType(value="field")
   private Date date;
   
   public Date getDate() {
      return date;
   }
   
   public void setDate(Date date) {
      this.date = date;
   }
}

@Entity
public class Quotation extends SubDocument {
    @Temporal(TemporalType.DATE)
    @AccessType(value="field")
   private Date date;
   
   public Date getDate() {
      return date;
   }
   
   public void setDate(Date date) {
      this.date = date;
   }
}

@Entity
public class MasterDocument implements Serializable {
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @AccessType(value = "field")
   private Long id;
   
   @ManyToOne
   @AccessType(value = "field")
   private SubDocument subDocument;
   
   public Long getID() {
      return id;
   }
   
   public SubDocument getSubDocument() {
      return subDocument;
   }

   public void setSubDocument(SubDocument subDocument) {
      this.SubDocument = subDocument;
   }
}


Now, if you try to query in HQL like this,

Code:
SELECT subDocument.date
  FROM MasterDocument AS masterDocument
  LEFT JOIN masterDocument.subDocument AS subDocument
  WHERE subDocument.date = :date


the results are invalid. Looking at the generated SQL points to the problem. It seems that every possible child class of SubDocument will be left outer joined, but the where clause will only verify on the first join table that does have the column "date". It's then missing on the other ones. The behavior is also inconsistent between different installations since the left outer join are not ordered in any particular way.

So my question is this, is there a bug in the SQL conversion? I know that the code design in this case is not something that would be recommended, but the fact is that HQL does permit this kind of query. I tested this behavior with MSSQL and Derby dialects and both resulted in the invalid result set.

Thank you all for your answers in advance.

N.B. If this was already documented elsewhere, please forgive me. I tried searching in the forums, but since I don't really know how to describe the problem, no satisfactory results where found.


Top
 Profile  
 
 Post subject: Re: Possible bug in HQL?
PostPosted: Thu Feb 18, 2010 10:58 am 
Newbie

Joined: Thu Feb 18, 2010 10:30 am
Posts: 1
Hi

Similar 'problem' here. It happens when 2 derived classes have a field with the same name. Hibernate includes only the first class (what ever it is) in the where statement. I think an or-statement for all classes with a matching field should be generated instead.



Update:

After spending a little bit more time on it:

  • it may work and give you the expected results if both fields (with the same name) are of the same type
  • but what should happen if the two fields are of different type?
  • subDocument returns objects of type SubDocument. And SubDocument does not have a field date. Should't the query throw an exception?

At the end, I think, there is no logical solution that works in all situations. Moving the
date field to class SubDocument (if this is valid from a business logical point of view) or renaming the fields may solve the problem.


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.