<p>What is the best way to save enums into a database?</p>
<p>I know Java provides <code>name()</code> and <code>valueOf()</code> methods to convert enum values into a String and back. But are there any other (flexible) options to store these values?</p>
<p>Is there a smart way to make enums into unique numbers (<code>ordinal()</code> is not safe to use)?</p>
<h4>Update</h4>
<p>Thanks for all awesome and fast answers! It was as I suspected.</p>
<p>However, a note to <a href="https://stackoverflow.com/users/3295">toolkit</a>: That is one way. The problem is that I would have to add the same methods to each enum type that I create. That's a lot of duplicated code and, at the moment, Java does not support any solutions for this (a Java enum cannot extend other classes).</p>

I would argue that the only safe mechanism here is to use the String
```
name()
```
value. When writing to the DB, you
could
use a sproc to insert the value and when reading, use a View. In this manner, if the enums change, there is a level of indirection in the sproc/view to be able to present the data as the enum value without "imposing" this on the DB.