0

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

  1. Why is the value of menu_type for the fourth row displaying zero even though it's not actually zero.
  2. 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?
  3. If there's an easier way to do this (initialize menuType through my sql file) please suggest. I'm using mysql database.

Thanks

pdm
  • 23
  • 1
  • 8
  • If even hibernate had to create the table, for enum types by default it will set integer data type in databases, and this column would mean the order of the enum type, i think. So I suggest you to use not integer value, but instead string value of enum. It would be meaningful in database and in application. To say hibernate to use enum to resolve by string value. put `@Enumerated(EnumType.String)` – ernestk Feb 23 '17 at 15:12

1 Answers1

0

0 because your number is not the enum.ordinal(). This is a number, the position in the list, starting at 0.

You cannot save ComplexObjects in databases. Therefore eventually everything has to become strings and numbers (and dates, blobs, etc). Only what the database knows. Your database does not know your newly created enum. It can only persist a STRING or NUMBER which will be translated to your enum by Hibernate.

Could I give another advise?:

@Enumerated(EnumType.ORDINAL)
  • EnumType.ORDINAL is the default, so you could have left this out.
  • If you ever choose to add another value inbetween, the ORDINAL changes, and you will get different enum values back from your model
  • If you use EnumType.STRING you will get the text version saved. If you then add another value inbetween, you have no problems.
Jeroen van Dijk-Jun
  • 1,028
  • 10
  • 22
  • Is there an annotation I can use to automatically save the number in brackets such as 1 for CATEGORY instead of using EnumType.ORDINAL? – pdm Feb 24 '17 at 05:53
  • No you can't, but there is a workaround possible. It's explaned here: http://stackoverflow.com/questions/5372855/can-i-specify-ordinal-for-enum-in-java#5372902 – Jeroen van Dijk-Jun Feb 24 '17 at 09:50