I'm having trouble working with enum types in the database. I have an Entity,
@Entity
@Table(name="menu")
public class Menu {
@Id
@GeneratedValue
@Column(name="pk_menu_id")
private int menuId;
@Column(name="menu_name")
private String menuName;
@Column(name="menu_desc")
private String menuDesc;
@Enumerated(EnumType.ORDINAL)
@Column(name="menu_type")
private MenuType menuType = MenuType.UNCLASSIFIED;
...
}
and an enum type
public enum MenuType {
CATEGORY(1),
ADMIN(2),
UNCLASSIFIED(3);
private int menuType;
...
}
When i create a new menu this is what the table looks like menu table
The fourth row was inserted through the application like this
private Menu createMenu(String title, String description, String url,
String icon,String menuType){
Menu menu = new Menu();
menu.setMenuName(title);
menu.setMenuDesc(description);
menu.setUrl(url);
menu.setIcon(icon);
menu.setMenuType(MenuType.UNCLASSIFIED);
iMenuService.insertMenu(menu);
...
}
The first three rows were inserted through hibernate initialization
<!-- Use this script to initialize the database -->
<property name="hibernate.hbm2ddl.import_files">ad_post_init.sql</property>
I have three questions
- Why is the value of menu_type for the fourth row displaying zero even though it's not actually zero.
- Is there a way to initialize menu_type column through my init file
ad_post_init.sql
, so that menu_type column contains the enum type MenuType? I want to understand what hibernate actually does when it writes menuType to the database and how it's able to give me the correct value of menuType when I retrieve the object in my code. If I was to copy what hibernate does what would my insert statement look like? - If there's an easier way to do this (initialize menuType through my sql file) please suggest. I'm using mysql database.
Thanks