-->
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: many-to-one - HQL problem
PostPosted: Fri May 29, 2009 3:38 pm 
Newbie

Joined: Fri May 29, 2009 2:46 pm
Posts: 2
I have defined <many-to-one> relation for RewardsID in Points History Table as shown below in .hbm.xml file.

My requirement is to retrieve points history between particular dates. Given below is the HQL that I am using.
select
pnt.createdDate,
pnt.points,
pnt.rewardsChart.name,
pnt.rewardsChart.description,
from PointsHistory pnt where pnt.createdDate between ? and ?


The problem that I am facing with this query is that it only returns the rows from points history table where RewardsID is not null. Is there any way in which we can retrieve all the desired rows?

Given below are the mapping files and query along with the problem/question that i have:

RewardsChart Table
RewardsID – Primary Key
Name
Description
.
.
.

Points History Table
PointsHistoryID - Primary Key
UserID
Points Awarded/Redeemed
RewardsID – nullable foreign key. If Points have been redeemed, thn this column would be populated with the RewardsID from Rewards Table (given above). If points have been awarded, thn this column would have null value
CreatedDate
.
.
.
.

RewardsChart.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="xxx.RewardsChart"
table="RewardsChart">
<cache usage="read-write" />
<id name="rewardID" type="integer">
<column name="RewardsID" />
<generator class="identity" />
</id>

<property name="name" type="String">
<column name="Name" />
</property>

<property name="description" type="String">
<column name="Description " />
</property>
</class>

</hibernate-mapping>

PointsHistory.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="xxx.PointsHistory"
table="PointsHistory">
<cache usage="read-write" />
<id name="pointsHistoryID" type="integer">
<column name="PointsHistoryID" />
<generator class="identity" />
</id>

<many-to-one name="userID" column="UserID" />

<property name="points" type="long">
<column name="Points" />
</property>

<many-to-one name="rewardsChart" column="rewardsID" not-found="ignore" not-null="false" />

<property name="createdDate" type="timestamp">
<column name="CreatedDate" />
</property>
</class>

</hibernate-mapping>


If i remove following fields from the query, it works as desired:
pnt.rewardsChart.name,
pnt.rewardsChart.description,


Thanks in advance...


Top
 Profile  
 
 Post subject: Re: many-to-one - HQL problem
PostPosted: Sat May 30, 2009 7:38 am 
Senior
Senior

Joined: Thu Jan 08, 2009 3:48 pm
Posts: 168
You should use an outer join for this
Code:
select
pnt.createdDate,
pnt.points,
rc.name,
rc.description,
from PointsHistory pnt
left outer join pnt.rewardsChart rc
where pnt.createdDate between ? and ?


Top
 Profile  
 
 Post subject: Re: many-to-one - HQL problem
PostPosted: Mon Jun 01, 2009 11:04 am 
Newbie

Joined: Fri May 29, 2009 2:46 pm
Posts: 2
It worked...Thanks for help...Really appreciate it...


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.