database - Use Enums instead of foreign keys -



database - Use Enums instead of foreign keys -

after using hibernate enum mapping 1 time again today wondering whether thought stretch little bit more. explain thought came from: of course of study aim normalized info model our applications results in fact, alot of tables contain category, state or similar data. these tables have few columns (often pk , 1 or 2 content columns) , rows. also, content of tables changes rarely, never. if we'd utilize enum , map table ordinal or integer (both hibernate, i'd orm can that), wouldn't improve in both performance (less joins) , handling (enums in java can used elegantly)?

to clarify things bit:

table persons id: number name: varchar relationship_status_id: number

table relationship_status id: number status: varchar

content persons: 1 | john doe | 1 2 | mary poppins | 2

content relationship_status 1 | single 2 | married

now i'd dump status table, have 2 status in enum , map column ordinal. senseful thing do? interested if kind of design improve performance-wise.

my factors choosing between table , enums following:

the list of possible values alter in future, , don't want recompile, retest , redeploy app when happens: utilize table the list of possible values alter in future, every value of table used in code implement business logic (like if status == married else something): we'll need alter logic anyway if list of possible values change, utilize enum the list never, ever change: utilize enum

you can still maintain table, , utilize enum in code, though. makes clearer when looking @ info in database, when don't know how enum implemented. 0 meaning married , 1 meaning single not obvious @ all. if maintain table reference, can @ to the lowest degree figure values mean, , create sure it's not possible insert 2 or other number in data.

another way utilize name of enum rather ordinal. takes bit more space , bit less efficient, makes info clearer , simpler analyze. lose safety, though, unless add together checked constraint.

database hibernate enums foreign-keys

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -