Mapping enums onto database in grails
My grails app uses a lot of enums, but the current GORM solution for enums isn't very satisfactory because it implies the data is in开发者_开发知识库 the code, since GORM doesn't map enums into the database.
Previously we used a big table containing different types of enumerations, e.g.: county and city selection in one table. Hence the table in database would contain 3 columns: the enum type (county or city), id and name (which county or city).
Right now I'd like to switch them to real enums, but it just seems unlikely with GORM. I tried to do it with transients in the domain class, but AFAIK it still requires you to have a static enum already present for the class to refer to.
I'm completely lost here, could anybody hint me from where or which function I might get what I wanted ?
Simply, do not use Enums for data which is not static and unlikely to change. Instead, use regular domain classes and bootstrap the required data.
Maybe this code is what you want:
enum UserOrderState
{
WAITCHECK("Ожидает проверки"),
INWORK("Выполняется"),
DONE("Выполнен"),
CANCELED("Отменен"),
NOPRODUCT("Нет в наличии")
String name
UserOrderState(String name)
{
this.name = name
}
static list()
{
[ WAITCHECK, INWORK, DONE, CANCELED, NOPRODUCT ]
}
}
Then in .gsp you can make combobox this way:
<g:select
name="state"
from="${internetshop.UserOrderState.list()}"
value="${fieldValue(bean: order, field: 'state')}"
optionValue="name"
/>
精彩评论