-->
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.  [ 3 posts ] 
Author Message
 Post subject: hibernate join, can't convert sql to hql
PostPosted: Fri May 04, 2007 6:29 am 
Newbie

Joined: Fri May 04, 2007 5:48 am
Posts: 2
hi. im using mysql db. i have to tables in database : employees and registration. in registration table are written employees registered working hours(registering by projects tasks). hours are registered every day. i need to make query which returns a list of all employees and their registered hours during some period. if employee didnt registered hours during that period, query should return employee and null. i can write this query code in sql, but i cant make it work in hql. here is sql which works fine:
Code:
SELECT Employees.firstname, sum(reg.hours)
FROM (select * from registration where registration.date
BETWEEN "2007-01-01" and "2007-01-31") as reg
RIGHT JOIN employees
ON Employees.id=reg.EmployeeID group by Employees.id;


here are my tables in mapping file:
Code:
<hibernate-mapping package="ideja.insight.domain" default-lazy="false">
...
<class name="Employable" table="employees" polymorphism="explicit"
      discriminator-value="BASE" abstract="true">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <discriminator column="discriminator" type="string"/>
      <property name="firstName" column="firstname"/>
      <property name="lastName" column="secondname"/>
      <property name="email" column="email"/>
      <property name="login" column="login"/>
      <property name="password" column="password"/>
      <property name="type" column="type" />
      <many-to-one name="profile" column="resourceprofileid"/>
      <subclass name="Employee" discriminator-value="EMPL">
      </subclass>
      <subclass name="Applicant" discriminator-value="APPL">
      </subclass>
</class>

<class name="Registration" table="registration">
      <id name="id" column="id">
         <generator class="identity"/>
      </id>
      <property name="date"  column="date" />
      <property name="hours" column="hours"/>
      <property name="freezed" column="freezed"/>
      <many-to-one name="employable" column="employeeid"/>
      <many-to-one name="projectsTasks"           column="projectstasksid"/>
</class>   



employable is parent class for employee and applicant.
i tried the fallowing hql query:

Code:

HibernateTemplate template = getHibernateTemplate();
List result = template.find("

select b.id, sum(reg.hours) from Registration reg " +
"right join reg.employable b " +
"where reg.date is null or reg.date
between '2007-01-01' and '2007-01-31' " +
               "group by b.id
");


The problem is: if some employee registered only for example on 2007-02-15 date. this query would not return this employee at all( i expect it to return this employee id and null for sum(reg.hours).
If there are no entries in registration table at all it returns all employees and null hours for everybody(works fine).
if there are registered hours during specified period in registration table, it works fine too.
So the problem is that only if employee has registered hours on different period than specified, this query doesn't return this employee at all.
im new to hibernate, and i dont understand why if i dont specify "reg.date is null" in where clouse it only returns matching rows like inner join in sql(in registration and emplloyes ). so maybe someone knows how to convert sql that i specified above to fully working hql[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 10:20 am 
Newbie

Joined: Mon Apr 30, 2007 12:27 pm
Posts: 18
I dont know if this is what you need, but if you want to add a restriction in your join, either left or right, you use the keywork "with" instead of where.

Check out, the hibernate documentation Chapter 14. HQL: The Hibernate Query Language.

In part 14.3. Associations and joins says:

" You may supply extra join conditions using the HQL with keyword.

from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight > 10.0

"

I hope this helps.
Regards, Javier Rivas.


Top
 Profile  
 
 Post subject: Thanks for your suggestion
PostPosted: Tue May 15, 2007 8:41 am 
Newbie

Joined: Tue May 15, 2007 8:30 am
Posts: 1
Your suggestion has helped me very much ,Thanks.
BUT....!
There is an other Big Problem:
Although Hibernate 3.1.2 Accepts the HQL :

from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight > 10.0

Hibernate 3.2.1 decline it:org.hibernate.hql.ast.InvalidWithClauseException: with-clause expressions did not reference from-clause element to which the with-clause was associated

Any Idea?


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