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 @entity annotation role. 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 in values() array (if you're using @enumerated(enumtype.ordinal) annotation). either way, never reference additional table. mapping (@enumerated(enumtype.string)) it's pointless have roleid begin with.

if want use entity

  • make role true entity - pojo getters / setters , identifier. may or may not mutable depending on want.
  • map 'roles' table.
  • reference @manytoone other tables.
  • you have populate table yourself; there's no built-in way hibernate you.

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -