> The whole N+1 select problematic is resolved in Squeryl by *not*
> using relations, but by using a query instead :
>
> from(one, many)((o,m) =>
> where(o.pKey === m.fKey)
> select((o, m))
> )
>
> This reminds me that there remains an optimisation to be made in the
> above query :
>
> Should the "one" side instantiated "many" times ?
I'm not sure... From relational viewpoint, we have many separate rows
and thus many "one" instances. As for me, it would be easier to
understand and remember (no... I should say "one special case less to
remember"). If we have immutable entities, there's no difference in
fact, so it's totally up to you. So we are to listen guys preferring
mutable entities.
Another question here is: do you have reliable way to determine when
this optimization is applicable? I think only if you got one-to-many
relation declared in schema.
> The way I see relations in Squery is that they are totally lazy, [...]
> in a single call, we do it with a query instead of having [...]
I see. Good. At least, simple. :) There's a thing called Hibernate I
hate for all those lots of tweaks impossible to remember and to use
together without headache.
A bit of philosophy (somewhat offtopic). I don't participate in that
discussion about advantages of immutability on scala mailing list
(because I have to read it before answering and I'm too lazy for that),
so I'll write here. I found immutability and functional approach work
great with anemic programming model and transaction script. The idea of
anemic is that data is passive, all actions are done by controller
objects which pass data to each other ("data floating by pipes"
metaphor). That is, data and logic are separated (yes, violation of OOP,
but rich programming model almost always violates SRP). In my case,
ideal HTTP request processing is this: when request comes, I get all
data in one place (with maximally optimized queries), pass that data to
BL controllers (which act as immutable functions) and write results back
to database in one place (that's great because this "one place" which
reads and writes data is DAL which implementation can contain
transparent caching). So, as I said before, this Squeryl feature we
discuss is not of much use for me: I'd always prefer writing queries by
hand to have maximum control over generated SQL. Thanks to strict
typization, that's now simple and reliable. (In fact, C# programmers got
LINQ and leveraged advantages of anemic model long ago.)
On the other hand, I'm just talking with a guy who loves laziness and
direct access to child collections, hates to remember join points and to
write queries by hand, and does not care about query optimizations. So
he will love this new feature for sure. :)