-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: HQL Recent Date Question
PostPosted: Fri Sep 26, 2003 5:43 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
Sorry if this is a silly question. I want to retrieve objects from the database where I get last last two months of the objects (based on a property of the object). For example, if it's August, and there are objects in the database for April, May, June and July (but not August), I want to retrieve the objects from June and July. Is it possible to write an hql query which will do this in one step (without the particulary months being hard-coded)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 8:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
But of course. Asuming the java class is MyClass with a property myDateProperty, how about something like:
Code:
Date start = ...;
Date end = ...;
Query qry = session.createQuery("from MyClass as c where c.myDateProperty between :rangeStart and :rangeEnd");
qry.setDate("rangeStart", start);
qry.setDate("rangeEnd", end);
...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 10:11 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
Sorry - I need to do that, but I'm trying to do it with a collection. I know I can set a "where" for retrieving the elements of a collection, but how can I specify the start and end dates in the collection mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 11:34 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
If that isn't possible, is it possible to restrict the number of objects returned by the database in the "where" attribute of a collection mapping? So I could, say, sort the results by date, but only retrieve the last 25 from the database?

Thanks for your help so far.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 26, 2003 11:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Ar you talking about in the mapping file? The where clause on the collections elements cannot be parameterized there.

You could set this to be a lazy collection and then filter the collecton based on the dates you want using session.filter();


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 12:35 am 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
Yes, I meant in the mapping file.

Wouldn't filter() load the data from the database, then filter it? (I realized applying a filter doesn't immediately load the lazy collection, but when it is loaded...)

I apologize, I guess I should have explained myself better in the first place - I have a collection of link objects in the database. However, for most of my pages I only want to display the links from the last two months that have links. I thought that perhaps I could store them all in one collection in the database, but only load the ones I was going to display as one collection, but still have the other accessible as another collection.

On another note, you guys are great with this message board. Really, getting a response within a few hours on an free$, open-source project is really something. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 7:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
no, of course not. The whole point of filter() is that it allows _efficient_ filtering, at the database. Please read the Hibernate manual.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 3:46 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
I did read the javadocs. Session.filter() says:

Filters allow efficient access to very large lazy collections. (Executing the filter does not initialize the collection.)

About efficiency. Nothing about how the data is loaded from the database. :-(

I take it then, that there's no way to limit the number of results returned with the "where" attribute on a collection in a mapping file?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 3:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Having limits on the number of returned objects in the "where" attribute just don't make sense - does it ?

Are you maybe instead looking for session.createFilter(Object collection, String filter) ? This one would allow you to get a Query object back on which you can do setMaxResultSize() etc..

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:25 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
I was looking for a limit on the number of returned objects so I could have a collection of only the recent objects, but I see using filter and the Lifecycle interface might allow me to load only the recent objects as well.

But that returns me to my original query question. Is it possible to write a query like:

from LinkDAO where
dateCreated between max(LinkDAO.dateCreated) and
(max(LinkDAO.dateCreated) - 2 months)

?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
of course. this is what subqueries are for.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:40 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
1. How on earth can you do "max(LinkDAO.dateCreated) - 2 months", even with a subquery?
2. I can't use subqueries because I'm using MySQL, which doesn't support subqueries :-( Is there any other single statement way?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 4:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Code:
from LinkDAO link
where link.dateCreated between ( select max(m.dateCreated) from LinkDAO m )
and ( ( select max(n.dateCreated) from LinkDAO n ) - 2 months )


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 6:13 pm 
Beginner
Beginner

Joined: Mon Sep 15, 2003 6:55 pm
Posts: 29
That doesn't work. I'm pretty sure "2 months" isn't something you can put in an sql statement. And even if it was, that produces sql which uses subqueries (yes, I tried it). But let's just say screw it, I'll use 2 queries - 1 to retrieve the date of the most recent entry, and the other will retrieve those entries.

Now I should have a lazily loaded collection with all the Link objects in it called AllLinks. And I'd like to use createFilter to initialize a second, non-persisted collection of the recent objects from the first collection called NewLinks. And I'd like to do that automatically when the object loads, because my web application follows a LoadData-DisplayData pattern. However, this appears to be impossible since the Lifecycle.onLoad() docs state: "It is illegal to access the Session from inside this method".

Is there actually a way to query the database automatically when an object is loaded?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 27, 2003 6:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Of course it produces subselects! (Actually, subselects work on some recent versions of MySQL.)

On some databases you can say things like " - 2 months". I assumed you knew your own equivalent platform syntax.

Quote:
Is there actually a way to query the database automatically when an object is loaded?


It is legal to re-enter the session in a UserType.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.