ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ole Trenner <ole.tren...@googlemail.com>
Subject Re: best way to use a Map?
Date Sat, 06 Oct 2007 09:44:09 GMT
(sorry if this message is a duplicate)

Rusty Wright wrote:
> I'm trying to figure out how to set up storing a Map of pairs of
> numbers.  I have a Photo object, and it has a Map of the different sizes
> in which the Photo is stored on disk.  The map keys are SIZE1, SIZE2,
> and SIZE3 (an enum; the map is an EnumMap).  The values in the Map are a
> simple object, ImageSize, with width and height fields. Each photo has
> its own sizes; no relationships and the widths and heights are
> essentially random values.
> 
> I'm thinking that in my database, each row for a Photo will have columns
> size1_width, size1_height, size2_width, size2_height, etc.  Doesn't
> sound too elegant but I can't see any other way.


I'd prefer having a photo table without these sizes and storing them in
a different table with a foreign key relation to the photos table
instead. Something like that:

photos:

photo_id | photo_property_1 | photo_property_2 | ...

sizes:

size_id | photo_id - fk to photos(photo_id) | size_key | width | height

This way a change of the available number of sizes has no influence on
your database scheme.


> I can't see any way to get these widths and heights into and out of the
> database other than to have getters and setters for each one. In order
> to not clutter my Photo class with those getters and setters I was
> thinking of making a PhotoDto data transfer object that does have the
> getters and setters and to wrap a Photo object in that before I send it
> off to iBatis.


When loading the photo objects you could join the photo table and the
sizes table and use the groupBy attribute in the mapping to easily map
the collection property (that is what the sizes map is).

When storing you'd have to stick to two subsequent inserts (one for the
sizes, one for the photo itself.


Just my 2 ct...
Cheers,
Ole.


Mime
View raw message