Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[iBatis in Action] Table Relationships

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of my own personal weak points when it comes to iBatis is table relationships and mapping them in the mapping file. The documentation that is available does a decent job of explaining how this works. However, it also does what most documentation does. It shows you 5 ways to do the same thing. And you really don't know which way should be used.

Specifically I always have a problem when I have the following scenerio..




Does the book cover this type of scenerio and how to map these back to the appropriate objects? Thanks.
 
Larry Meadors
author
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason it doesn't tell you which way *should* be used is that because the way to do it depends on what you need.

Unlike most ORM solutions, it is not a one-size-fits-all sort of thing, where you get a way to do it, and that's it.

Let me explain. If you are looking at parts, you may want to see all of the items that use those parts...but you may not. If you don't there is no value in having those related objects queued up and ready to go.

So, iBATIS gives you at least 3 ways to deal with this:
1) lazy loading of composite objects (n+1 selects)
2) aggressive loading of composite objects (w/ 1 select)
3) "fat-map-mapping" ala Vic C.
4) However the heck you wanna do it

IMO, lazy loading (option #1) is sort of a retarded idea. You either need the data, or you don't. If you need it anyway, get it up-front with one hit to the database (instead of n+1), and be done with it (which is option #2). I know, I know...that won't work with ORM, but since iBATIS is not ORM, it *will* work.

Option #3 is the quick and dirty approach. An example might help make it clearer:

<select id="blah" resultClass="hashmap">
select
item_table.id as itemId,
item_table.name as itemName,
part_table.id as partId,
part_table.part as partName
from part_table p
join item_part_table ip on p.id = ip.part_id
join item_table i on ip.item_id = i.id
</select>

Running that will get you a List of Map objects that contain all the data together.

There is not a single right way to do it - there are several options, and each one is the right way in the right situation.

Larry


PS: Yes, we cover all of those..and maybe even a few others in the book. ;-)
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Larry. I appreciate the detailed response. I wasn't bashing the documentation giving several solutions to the same problem. I meant that it can be frustrating while learning the framework because you truly don't know how you need to do it for a given scenerio. It's mostly trial and error.

Does the book help guide th reader. Something like "in the folllowing situation......this technique is probably the best approach." Or is it still left to the reader to determine her own needs?

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic