Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populate database table with enum values in hibernate

is there any possibility to let Hibernate (3.6) populate a database table with values for a given enum ? I have the 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 this enum without any problems from another entity class using

@Enumerated(EnumType.STRING)
public Role getRole()

My question is, how can I populate the corresponding table ROLE automatically ? All the underlying logic and definiations resides in an XML specification. Of course, I can generate a sql file from this spec by XSL and let Hibernate import this by the import.sql sematic at startup... But is there a more elegant way ?

The table should look like this:

|RoleID|RoleName      |
|  0   |ROLE_USER_FREE|
....
like image 679
Dominik Avatar asked Apr 11 '11 15:04

Dominik


2 Answers

You have to pick a side - either you're going to use Role as enum or as entity. You're trying to do both and that's only going to lead to trouble along the road.

If you want to use enum

  • Remove @Entity annotation from Role. It's not an entity, it doesn't have a primary key. It also shouldn't be mutable, so there's little point in persisting it.
  • Remove Roles (or whatever it's called) table from the database. Hibernate persists enums by name (if you're using @Enumerated(EnumType.STRING) mapping) or by index in values() array (if you're using @Enumerated(EnumType.ORDINAL) annotation). Either way, it will never reference your additional table. With you mapping (@Enumerated(EnumType.STRING)) it's pointless to have RoleID to begin with.

If you want to use an entity

  • Make Role a true entity - POJO with getters / setters and identifier. It may or may not be mutable depending on what you want.
  • Map it to your 'Roles' table.
  • Reference it as @ManyToOne from your other tables.
  • You will have to populate the table yourself; there's no built-in way for Hibernate to do it for you.
like image 135
ChssPly76 Avatar answered Oct 17 '22 07:10

ChssPly76


This populates my database fine

public class Person implements UserAccount
{
    public static enum Role
    {
        anon(6), admin(1), standard(4), manager(3), user(5), director(2);
        private int weight;

        Role(int weight)
        {
            this.weight = weight;
        }

        public int weight()
        {
            return weight;
        }
    }

    @ElementCollection(targetClass = Role.class, fetch=FetchType.EAGER)
        @Enumerated(EnumType.STRING) // Possibly optional (I'm not sure) but defaults to ORDINAL.
        @CollectionTable(name="person_roles")
        @Column(name="role") 
    public Set<Role> getRoles()
    {
        return roles;
    }
 ...
like image 4
Ken Avatar answered Oct 17 '22 07:10

Ken