-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem converting existing SQL query into HSQL query
PostPosted: Mon Apr 06, 2009 1:09 pm 
Newbie

Joined: Mon Apr 06, 2009 11:46 am
Posts: 1
I am converting an existing project over to using Hibernate by converting existing JDBC calls into HSQL and I have run into SQL query that I cannot figure out how to do in Hibernate. I have spent 2 days trying to figure this out with no luck.

I am trying to select all current form version (i.e. max form version) with a past release date. (A form with a release date in the future means the form has not yet come into effect).

Here is an example of the data in the database:

Code:
TEMPLATE_FORM Table:
templateId  formType  formVersion  releaseDate
==============================================
1           Form1     1.0          2009-03-12
2           Form2     1.0          2009-03-13
3           Form1     1.1          2009-04-05
4           Form2     2.0          2009-06-01  (Future date)


Here is the expected result.

Code:
RESULTS OF QUERY:
templateId  formType  formVersion  releaseDate
==============================================
3           Form1     1.1          2009-04-05   (NOTE: version 1.1 supercedes 1.0)
2           Form2     1.0          2009-03-13   (NOTE: version 2.0 is not yet in effect)


I am familiar with SQL but am new to Hibernate. In SQL this would look like:

SELECT T2.template_id, T2.form_type, T2.form_version, T2.release_date FROM
(
SELECT form_type, max(form_version) AS form_version FROM Pp_Template_Form WHERE release_date <= sysdate GROUP BY form_type
)
T1 INNER JOIN Pp_Template_Form T2 ON T1.form_type = T2.form_type AND T1.form_version = T2.form_version ORDER BY form_type ASC


From what I can tell, Hibernate doesn't seem to support joins on subqueries. This certainly did not work:

from ( select formType, max(formVersion) from FwTemplateForm where releaseDate <= current_date() group by formType ) t1 inner join template.self t2 on t1.formType = t2.formType and t1.formVersion = t2.formVersion order by formType asc

Produces the following exception:
org.hibernate.hql.ast.QuerySyntaxException: unexpected token: ( near line 1, column 6 [from ( select formType ...

I have figured out how to do joins... and I have figured out how to do subqueries... but I have not figured out how to do both joins and subqueries at the same time.

Is this query possible to do in Hibernate (without using native SQL)? Anyone have any suggestions on how I should do this?

The mapping I am using is:
Code:
<hibernate-mapping>
   <class name="com.forms.framework.db.bean.FwTemplateForm" table="FW_TEMPLATE_FORM">
      <id name="templateId" column="TEMPLATE_ID" type="java.lang.String">
         <generator class="uuid" />
      </id>
      <property name="formType" type="java.lang.String">
         <column name="FORM_TYPE" length="32" not-null="true" />
      </property>
      <property name="formVersion" type="java.lang.String">
         <column name="FORM_VERSION" length="32" not-null="true" />
      </property>
      <property name="releaseDate" type="java.util.Date">
         <column name="RELEASE_DATE" not-null="true" />
      </property>
      <set name="self">
         <key>
            <column name="TEMPLATE_ID" length="36" not-null="true" />
         </key>
         <one-to-many class="com.forms.framework.db.bean.FwTemplateForm" />
      </set>
   </class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.