Populate database table with enum values in hibernate -
is there possibility let hibernate (3.6) populate database table values given enum ? have following class:
@entity public enum role { role_user_free("role_user_free"), role_user_standard("role_user_standard"), role_user_premium("role_user_premium"), role_admin("role_admin"); ... constructor / setter / getter etc. } i can use enum without problems entity class using
@enumerated(enumtype.string) public role getrole() my question is, how can populate corresponding table role automatically ? underlying logic , definiations resides in xml specification. of course, can generate sql file spec xsl , let hibernate import import.sql sematic @ startup... there more elegant way ?
the table should this:
|roleid|rolename | | 0 |role_user_free| ....
you have pick side - either you're going use role enum or entity. you're trying both , that's going lead trouble along road.
if want use enum
- remove
@entityannotationrole. it's not entity, doesn't have primary key. shouldn't mutable, there's little point in persisting it. - remove
roles(or whatever it's called) table database. hibernate persists enums name (if you're using@enumerated(enumtype.string)mapping) or index invalues()array (if you're using@enumerated(enumtype.ordinal)annotation). either way, never reference additional table. mapping (@enumerated(enumtype.string)) it's pointless haveroleidbegin with.
if want use entity
- make
roletrue entity - pojo getters / setters , identifier. may or may not mutable depending on want. - map 'roles' table.
- reference
@manytooneother tables. - you have populate table yourself; there's no built-in way hibernate you.
Comments
Post a Comment