Better way to implement reverse mapping of custom enum ordinals?

L

Lew

Tom said:
Whoops, you're quite right, it should.

Aside: there is at least one approach to Hibernate serialization of enums
where the programmer provides for reflective use the names of the instance
method to convert an enum to an external value, and the static method for the
other direction. (IIRC they default to 'toString()' and 'valueOf()'.)
 
D

Daniel Pitts

Lew said:
But it's a type-safe int!
Actually, the way I see it is the public integer is an *external* key.
It is not controlled by the program, and therefor must be declared as a
magic number somewhere. The fact that it is being converted to and from
type-safe enums is at least a partial win. The best practices for
this situation would be to maintain the enum value for as much of the
time as possible.

For better control over this "best practice", it might make more sense
to make that a package protected value, and force the data-access code
to do the conversions immediately after read, and prior to write.
 
A

Arne Vajhøj

Martin said:
david.karr wrote:
Quite often databases will have columns that are stored as integers,
but represent enumerated values. In object-relational mapping, it's a
good idea to translate that integer value to the enumerated value it
represents.
[ SNIP ]

It's an even better idea to store the enumeration "name" as a varchar.
Agreed - and hold the integer:name mapping as a separate table.

Or don't even bother with integers in the database at all, but just use
the VARCHAR value.

Depending on the database using int referring with a table mapping
to varchar may provide better possibilities for constraints
in the database.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top