开发者

One Table for all enum types in Hibernate

I have a Class let say Employee as follows;

public class Employee{
    ... Other attributes ...
    @Column @Enumerated(EnumType.Ordinal)
    private Gender gender;

    @Column @Enumerated(EnumType.Ordinal)
    private EmployeeType employeeType;
    ... Getters & Setters ...
}

My Employee table is something like this.

Employee{
    ... other columns ....
    gender int,
    employeetype int,
    ...
}

and I have a gene开发者_如何学Cric lookup table lookups

Lookups{
    id long,
    lookName String,
    lookId int,
    lookValue String
}

I want Genders, and EmployeeTypes to be persisted in Lookups table by hibernate as follows, How can I do that;

+--+-------------+---------+------------+ 
|ID|LOOKUPNAME   |LOOKUPID | LOOKUPVALUE|
+--+-------------+---------+------------+
+--+-------------+---------+------------+
|1 | GENDER      | 1       | MALE       |
+--+-------------+---------+------------+ 
|2 | GENDER      | 2       | FEMALE     |
+--+-------------+---------+------------+
|3 | EMPLOYEETYPE| 1       | TEAM MEMBER|
+--+-------------+---------+------------+
|4 | EMPLOYEETYPE| 2       | TEAM LEADER|
+--+-------------+---------+------------+
|5 | EMPLOYEETYPE| 3       | MANAGER    | 
+--+-------------+---------+------------+    


Create the Lookups class. Then, for the Gender and EmployeeType fields of Employee class, replace them with a Lookups field. Then remove the Enumerated annotations.


What i understand is, you want to store all your constants/Enums in one table only in automated way.

To do this, you can have

  • Some XML/property file, which will be loaded on server startup to read this data.
  • The class, which will read property/XML file on server startup and check if lookup data exists or not.
    • If not, then it will persist the data in the DB.
    • If data is there, set some flag on DB. If this flag is true, the startup class will not invoke the check again on next server startup.

This way, the startup class can check the data at startup and insert it if its not there.


perhaps you could put them together as follows and persist them?

import java.util.*;
enum Gender {
    male, female
}
enum EmployeeType {
    member, leader, manager
}
class Lookups {
    public String toString() {
        return id + " " + lookName + " " + lookId + " " + lookValue;
    }
    private Lookups(Enum e) {
        id = ++n;
        lookName = e.getClass().getName();
        lookId = e.ordinal() + 1;
        lookValue = e.name();
    }
    private static void create(Enum e) {
        for (Object o : e.getDeclaringClass().getEnumConstants())
            lookups.add(new Lookups((Enum) o));
    }
    final long id;
    final String lookName;
    final int lookId;
    final String lookValue;
    static long n;
    static Set<Lookups> lookups = new LinkedHashSet<Lookups>();
    static {
        create(Gender.values()[0]);
        create(EmployeeType.values()[0]);
    }
}
public class Main {
    public static void main(String[] args) {
        for (Lookups lookups : Lookups.lookups)
            System.out.println(lookups);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜