Adding a natural key to a database table using straight GORM

I wanted to create a table of US Zip code data.

Just the 5, not the 5+4 or 5+4+2 variety…

Something that would house this kind of data.

I noticed my buddy Duncan Sommerville had written a post when I did a Google on this, but I wanted to give an alternative solution to this based on something I’d read in Robert Fischer’s GORM + GSQL book (P73-74).

I adapted this to give it my own twist so I could use the name ‘zipcode’ for the id, based on something straight out of the Grails reference. (See also Hibernate generators)

I experienced difficulty getting the ‘length:5’ constraint to play ball in generating a MySQL schema based a 5 character key, per DGG 2nd ed P 47, so I’d end up with a five character natural key, but found specifying the constraint of ‘size:5..5′ worked a treat.

USZip

USZip schema

USZip indices showing zip as primary

For the inquisitive amongst you, my odd class USZip100sRange is based on this data.

Hope you Enjoy! :-)

Advertisements

About this entry