-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with obtaining last item of the list.
PostPosted: Wed Jun 08, 2011 3:43 am 
Newbie

Joined: Wed Jun 08, 2011 3:29 am
Posts: 7
I've just written one of my first HQL-queries. It should look for such objects of Activity class (which contains list of objects of TodayEffort class) whose last item in todayEfforts-list has non-zero value of effort field.
Code:
SELECT DISTINCT a FROM Activity a JOIN a.todayEfforts tes WHERE ... AND (SELECT tes.effort FROM tes WHERE tes.id=(SELECT max(tes.id) FROM tes))>0

But really it finds all objects of Activity class if last item in TODAY_EFFORT table has non-zero value of effort field, and doesn't find any objects otherwise.
Please help me to fix it


Top
 Profile  
 
 Post subject: Re: Problem with obtaining last item of the list.
PostPosted: Wed Jun 08, 2011 8:49 am 
Newbie

Joined: Mon Feb 21, 2011 6:57 am
Posts: 8
Hi!

I can't understand what's your final expectation....class Activity (A entity) has a list of class TodayEffort (B entity), you want to retrieve A instances whose property "effort" of last item of entities B related to A is not null...

Have you tried left outer joins, like "...on a.id = tes.activity" ?


Top
 Profile  
 
 Post subject: Re: Problem with obtaining last item of the list.
PostPosted: Thu Jun 09, 2011 10:15 am 
Newbie

Joined: Wed Jun 08, 2011 3:29 am
Posts: 7
AFAIU I can do it in standard SQL, but in HQL I have no access to "tes.activity" because tes (TodayEffort, B) doesn't have 'activity' field, this foreign key exists in the database only, class Activity doesn't have it therefore it's not mapped. I'd be happy if I had an access to foreign keys.

Let's say it in this way: There is a class A which contains a list of a class B. I want to get such instances of the class A which have such last items of their lists A.listOfB which have non-zero values of the field B.field1.
How can I do this?


Top
 Profile  
 
 Post subject: Re: Problem with obtaining last item of the list.
PostPosted: Thu Jun 09, 2011 10:24 am 
Newbie

Joined: Wed Jun 08, 2011 3:29 am
Posts: 7
Just want to add my mapping:
Code:
<hibernate-mapping>
    <class name="...model.entities.Activity" table="ACTIVITY" lazy="false">
        <id name="id" type="java.lang.Long" unsaved-value="-1">
        ...
        </id>
        ...
        <list name="todayEfforts" inverse="false" table="TODAYEFFORT" lazy="true">
            <key>
                <column name="ActivityID" />
            </key>
            <list-index column="idx" base="0" />
            <one-to-many class="...model.entities.TodayEffort" />
        </list>
    </class>
</hibernate-mapping>

Code:
<hibernate-mapping>
    <class name="...model.entities.TodayEffort" table="TODAYEFFORT">
        <id name="id" type="java.lang.Long">
        ...
        </id>
        <property name="date" type="java.util.Date" column="DATE" />
        <property name="effort" type="short" column="EFFORT" />
    </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Problem with obtaining last item of the list.
PostPosted: Sat Jun 11, 2011 3:33 am 
Newbie

Joined: Wed Jun 08, 2011 3:29 am
Posts: 7
I solved the problem:
Code:
SELECT DISTINCT a FROM Activity a JOIN a.todayEfforts tes WHERE ... AND a.todayEfforts[maxindex(a.todayEfforts)].effort>0

My main mistake of usage maxindex was that in the last condition I used tes instead of a.todayEfforts so I got errors. I don't know why it's so important. I'll be grateful if somebody tell me.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.