in-memory HQL query

34 views
Skip to first unread message

Jorge

unread,
Nov 12, 2010, 8:22:29 PM11/12/10
to nhusers
Hello!
Is it possible to take an HQL query and apply it to a collection of
entities in memory?
Something similar to LINQ?

Thanks!
Jorge

Jason Meckley

unread,
Nov 13, 2010, 8:09:06 AM11/13/10
to nhusers
that would be linq then :)

Jorge

unread,
Nov 13, 2010, 2:06:04 PM11/13/10
to nhusers
yeah, I guess I will store a linq query along with the HQL.
Problem is, LINQ cannot be serialized to disk, while HQL can.
So, if HQL could be converted to LINQ, then this would be the nicest
solution.

Stefan Wenig

unread,
Nov 15, 2010, 5:55:16 AM11/15/10
to nhusers
BTW, internally, LINQ is converted to HQL. There might be a way to
extract, save and execute this HQL.
> > > Jorge- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

José F. Romaniello

unread,
Nov 15, 2010, 7:26:00 AM11/15/10
to nhu...@googlegroups.com
Wrong. Linq is converted to an ANTLR expression tree, the same thing HQL is converted to.
But there is not conversion from Linq to HQL and it is not possible to tweek an ANTLR expression tree into HQL (not sure about this last thing.)

2010/11/15 Stefan Wenig <stefan...@rubicon.eu>

Jorge

unread,
Nov 15, 2010, 11:40:25 AM11/15/10
to nhusers
Interesting. I may use dynamic LINQ, and store the LINQ query as a
string.


On Nov 15, 7:26 am, José F. Romaniello <jfromanie...@gmail.com> wrote:
> Wrong. Linq is converted to an ANTLR expression tree, the same thing HQL is
> converted to.
> But there is not conversion from Linq to HQL and it is not possible to tweek
> an ANTLR expression tree into HQL (not sure about this last thing.)
>
> 2010/11/15 Stefan Wenig <stefan.we...@rubicon.eu>

Stefan Wenig

unread,
Nov 16, 2010, 11:12:31 AM11/16/10
to nhusers
You're right, I skipped a step. I once asked steve strong whether he
could emit the HQL as a string for diagnostics. He said: easily. (much
easier than serializing and deserializing LINQ in any case...)

but anyway, I digress.
> > - Zitierten Text anzeigen -- Zitierten Text ausblenden -

Throckmorton

unread,
Nov 16, 2010, 3:12:49 PM11/16/10
to nhusers
Wait a second, I'm confused.

a) I start with an HQL query, which is just a string.
b) NH is designed to apply this HQL to a database, by first converting
to ANTLR
c) I want to apply the HQL to an in-memory collection instead
d) so, I could use the LINQ equivalent, if it can be generated from
the HQL
e) since LINQ and HQL get converted to ANTLR expression, I would need

HQL ==> ANTLR ===> LINQ

f) this may be currently impossible

Fabio Maulo

unread,
Nov 16, 2010, 4:33:35 PM11/16/10
to nhu...@googlegroups.com
The tweet of few days ago

but I learned that lesson around 26 years ago from my professor of statistics

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Stefan Wenig

unread,
Nov 17, 2010, 3:52:35 AM11/17/10
to nhusers
I was just going off on a tangent. AFAIK, what you describe cannot be
done, unless somebody comes and builds it. Wouldn't hold my breath
though, HQL was not built for in-memory execution.

I'd rather go look for a way to serialize in-memory LINQ queries.

http://stackoverflow.com/questions/4036489/what-is-the-easiest-way-to-save-a-linq-query-for-later-use

Also, Silverlight RIA toolkit has code that does that. For remote
invocation, not storage, but hey...
problems:
- in RIA toolkit, serialization (client) and deserialization (server)
use similar, but different types.
- if you compile an IQueryable expression, it will compile to the
IQueryable and Queryable extension methods. you'd need to replace that
with IEnumerable and Enumerable extension methods so that they
actually do the work instead of just creating expression trees again.

Other stuff might be available. Tell us if you find a way!

HTH,
Stefan

Fabio Maulo

unread,
Nov 17, 2010, 6:19:36 AM11/17/10
to nhu...@googlegroups.com
WebORB
http://www.themidnightcoders.com/develop-ria/guided-tour/
do the same with HQL

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Throckmorton

unread,
Nov 17, 2010, 10:45:23 AM11/17/10
to nhusers
Thanks, Stefan. Will see what I can cook up.
But I am guessing that once you have the AST, it would be fairly
easy to generate the LINQ query. And, since Mono has implemented
LINQ, there may be some code I can scavenge there.

If this were possible, then one could really put the L2 query cache
on drugs: not only cache the query results, but also the LINQ
equivalent
of the HQL, so that the LINQ query could be *run on the entity cache*.

Which would mean that after an insert, the query could be updated
rather than
invalidated, as it is currently done.




On Nov 17, 3:52 am, Stefan Wenig <stefan.we...@rubicon.eu> wrote:
> I was just going off on a tangent. AFAIK, what you describe cannot be
> done, unless somebody comes and builds it. Wouldn't hold my breath
> though, HQL was not built for in-memory execution.
>
> I'd rather go look for a way to serialize in-memory LINQ queries.
>
> http://stackoverflow.com/questions/4036489/what-is-the-easiest-way-to...

Throckmorton

unread,
Nov 17, 2010, 10:46:22 AM11/17/10
to nhusers
Thanks. So it is not impossible :) at least not for WebORB.

On Nov 17, 6:19 am, Fabio Maulo <fabioma...@gmail.com> wrote:
> WebORBhttp://www.themidnightcoders.com/develop-ria/guided-tour/
> On Wed, Nov 17, 2010 at 5:52 AM, Stefan Wenig <stefan.we...@rubicon.eu>wrote:
>
>
>
> > I was just going off on a tangent. AFAIK, what you describe cannot be
> > done, unless somebody comes and builds it. Wouldn't hold my breath
> > though, HQL was not built for in-memory execution.
>
> > I'd rather go look for a way to serialize in-memory LINQ queries.
>
> >http://stackoverflow.com/questions/4036489/what-is-the-easiest-way-to...
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Fabio Maulo

unread,
Nov 17, 2010, 1:26:13 PM11/17/10
to nhu...@googlegroups.com
you will not gain something querying first the cacje and then the RDBMS where needed.
If you can accept "eventual consistency" queries avoiding the cache invalidation at each insert/update/delete what you are looking for is:
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.




--
Fabio Maulo

Throckmorton

unread,
Nov 17, 2010, 2:47:28 PM11/17/10
to nhusers
Thanks, Fabio. That's an interesting blog post, I will check out the
tolerant cache.

Could you explain a little why you don't think it is worth trying to
update a query in the cache?

I am thinking of the following tow situations:

A) Current Situation:

1) do expensive query on table FOO, (NH mapping class FOO_ENTITY) and
put query and entities returned in L2 cache
2) create new FOO_ENTITY
3) FOO_ENTITY is now cached in the entity cache
4) run query again: must hit RDBMS again

OR


B) With Query Updates:

1) do expensive query on table FOO, (NH mapping class FOO_ENTITY) and
put query and entities returned in L2 cache
2) create new FOO_ENTITY
3) FOO_ENTITY is now cached in the entity cache
4) do not invalidate the query, but instead run in-memory query on
FOO_ENTITY, and possibly add to cached query ids
5) no extra hit to RDBMS

My NH knowledge is very limited, so I am probably missing something
big here.

Cheers,
Jorge



> you will not gain something querying first the cacje and then the RDBMS
> where needed.
> If you can accept "eventual consistency" queries avoiding the cache
> invalidation at each insert/update/delete what you are looking for is:http://fabiomaulo.blogspot.com/2009/04/tuning-nhibernate-tolerant-que...
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Stefan Wenig

unread,
Nov 18, 2010, 3:13:00 AM11/18/10
to nhusers
from the HQL AST? that'd be hard. from the LINQ IQueryable expression
tree? probably.

but you need to watch out for cases where the behaviour of NH LINQ or
the DB differs from LINQ to objects. (null values, string comparison/
collation, ...?)

Stefan Wenig

unread,
Nov 18, 2010, 3:19:04 AM11/18/10
to nhusers
do what exactly? I suppose you're not talking serialize/deserialize
HQL? ;-)

convert HQL or IQueryable to LINQ 2 objects? execute HQL in memory?
couldn't find it in their docs.

On 17 Nov., 12:19, Fabio Maulo <fabioma...@gmail.com> wrote:
> WebORBhttp://www.themidnightcoders.com/develop-ria/guided-tour/
> On Wed, Nov 17, 2010 at 5:52 AM, Stefan Wenig <stefan.we...@rubicon.eu>wrote:
>
>
>
>
>
> > I was just going off on a tangent. AFAIK, what you describe cannot be
> > done, unless somebody comes and builds it. Wouldn't hold my breath
> > though, HQL was not built for in-memory execution.
>
> > I'd rather go look for a way to serialize in-memory LINQ queries.
>
> >http://stackoverflow.com/questions/4036489/what-is-the-easiest-way-to...
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com­>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/nhusers?hl=en.
>
> --
> Fabio Maulo- Zitierten Text ausblenden -

Throckmorton

unread,
Nov 18, 2010, 8:22:44 PM11/18/10
to nhusers
Yeah, is sounds like this is would be a big job.
I am going to look into implementing a poor-man's query engine for the
entity cache:
for each HQL query, work out a matching LINQ query and serialize it in
the L2 cache. This could be done
using dynamic LINQ, using a string, which should be not impossible to
serialize :)

Then, use this LINQ to query the database.

It would be real nice to automatically generate the LINQ, but one must
choose one's battles.


--Jorge

Stefan Wenig

unread,
Nov 19, 2010, 4:45:41 AM11/19/10
to nhusers
Keep the HQL, that's easy to serialize (string).
You can cache the LINQ query too, but no need to serialize it. Just
cache it in the app domain and rebuild it from HQL when needed. That
will make it a lot easier.

I still think converting HQL to LINQ is much harder than you believe.
After all, it's just what NH's LINQ provider does, just the other way
around. That was a lot of work, still in progress!

HTH,
Stefan

Throckmorton

unread,
Nov 19, 2010, 7:28:33 AM11/19/10
to nhusers
Yes, I meant to NOT automatically convert HQL to LINQ.
Rather, for an HQL query, if you can work out ahead of time
what the LINQ would be (not programmatically), then
use it. It doesn't have to be LINQ necessarily.
There is something called FLEE - Fast Lightweight Expresssion
Calculator
that could work in some cases, and it would be faster:

http://www.codeproject.com/KB/recipes/Flee.aspx

So, just have an interface class

class IMemQuery
{
public bool match(IEnumerable<T> entities);
}

and use whatever works for impl.
Reply all
Reply to author
Forward
0 new messages