Hi Andrew,
2013/3/27 Andrew Myers <[email protected]>:
> Hi,
>
> I've just found this project and I'm very excited about it, as I've fought
> against ORM's before and have had a lot of difficulty with them.
Thanks for your nice words
> I am just finding my way around jOOQ with a tiny database that contains two
> tables Country and City.
>
> With an ORM, I can display the info something lke this:
>
> for (Country c : countries) {
> // print country stuff
> for (City city : c.getCities()) {
> // print city stuff
> }
> }
>
> However it sucks, because it does a separate select for each City
Yes. jOOQ 2.x used to have generated fetchXXX() methods on generated
Record types. Your code would've looked like this:
for (CountryRecord c : countries) {
for (CityRecord city : c.fetchCityList()) {
// ...
}
}
In jOOQ 3.0, you can still use c.fetchChildren(...) methods for the
same. But as you say, this will suck as you'll issue a select per
country.
> jOOQ looks cool because I can do something like and get everything in a
> single select:
>
> Result<Record> result =
> create.select().from(COUNTRY).leftOuterJoin(CITY).onKey().fetch();
>
> To achieve someting similar I was able to do:
>
> Map<String, List<CountryRecord>> ctry =
> result.intoGroups(COUNTRY.CODE, CountryRecord.class);
> Map<String, List<CityRecord>> cities =
> result.intoGroups(COUNTRY.CODE, CityRecord.class);
> for (Map.Entry<String, List<CountryRecord>> entry :
> ctry.entrySet()) {
> CountryRecord cr = entry.getValue().get(0);
> // print country stuff
>
> List<CityRecord> thisCountriesCities =
> cities.get(cr.getCode());
> for (CityRecord c : thisCountriesCities) {
> // print city stuff
> }
>
>
> Unfortunately that'd be a bit ugly to do inside a JSP for example, whereas
> the first would be fairly straightforward as you could do a forEach with
> another nested forEach.
>
> Is this the best, or "right" way to do this? Or is there a nicer approach
> that I'm missing?
There had been many discussions about mapping joined relations to POJO
trees on this list. This feature has been postponed as Table <-> POJO
mapping wasn't the main focus in recent jOOQ 3.0 development. I'm not
excluding that it will be added in the near future, though.
Agreed, your approach using intoGroups() leads to a bit of
boiler-plate. Do you see any room for an improved API in that area?
What kinds of methods on org.jooq.Result were you missing? I'm very
open to add more convenience to Result, ResultQuery and other API
types.
> I'm just trying to find the happy medium I guess - I've struggled most of my
> career with the Object / Relational mismatch and I can't believe I'm
> actually wanting to force this stuff back into objects, but there you go!
In my opinion, jOOQ's table-oriented API will be quite nice to work
with, once Java 8 with all its lambda expressions and enhanced
Collections API is out. The problem is really that there reliable ways
of mapping joined tables to object graphs is very hard to do
automatically, while at the same time, it is very annoying to express
imperatively...
Cheers
Lukas
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.