Can the Mapper classes compose object heirarchies by querying other tables

384 views
Skip to first unread message

Mark Ashworth

unread,
Apr 12, 2013, 7:54:16 AM4/12/13
to jd...@googlegroups.com
Good day,

I was wondering if there was a way to compose object hierarchies using the Mapper interface. For example if I wanted to compose a Invoice object and the LineItems within the invoice when a select occurs against the invoice result set.

Regards,
Mark P Ashworth

Brian McCallister

unread,
Apr 12, 2013, 9:50:25 PM4/12/13
to jd...@googlegroups.com
it can be a part of it, you just need a reduce step afterwards. The easiest way is by fold(…) ing across the result. You can use Folder2, which gives you the raw result set, row by row, or Folder3 which allows you to map(…) before the fold and get the result of the map inside the fold() calls.

Basically, when folding, you get passed an accumulator (which was the return value from the last call, or the initial value passed in for the first call) and you add some state typically, and return it to go into the next fold() call. The final result is returned when you get to the end of the result set.

If your accumulator is a map of root objects in your graph, you can accumulate against it pretty reasonably.

-Brian


--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Danilo Reinert

unread,
Apr 13, 2013, 5:21:36 PM4/13/13
to jd...@googlegroups.com
How can one use Folder3 like this example: https://github.com/brianm/jdbi/blob/fold-sql-object/src/test/java/org/skife/jdbi/v2/sqlobject/TestFold.java ?

I couldn't realize how to Fold on an annotated query.

Brian McCallister

unread,
Apr 13, 2013, 6:30:36 PM4/13/13
to jd...@googlegroups.com
On Sat, Apr 13, 2013 at 3:21 PM, Danilo Reinert <danilo...@gmail.com> wrote:
How can one use Folder3 like this example: https://github.com/brianm/jdbi/blob/fold-sql-object/src/test/java/org/skife/jdbi/v2/sqlobject/TestFold.java ?

I couldn't realize how to Fold on an annotated query.

Ah, you cannot directly right now. Every attempt to make a non-horrible api for it has failed.

Your best bet is to iterate across (fold across) the per-row form in an concrete method on the sql object.

-Brian
Reply all
Reply to author
Forward
0 new messages