Hibernate 中所有枚举类型的一张表

发布于 2024-11-27 19:07:09 字数 1234 浏览 1 评论 0原文

我有一个类 Employee 如下;

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

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

我的员工表是这样的。

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

我有一个通用的查找表查找

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

,我希望将性别和 EmployeeTypes 通过 hibernate 保留在查找表中,如下所示,我该怎么做;

+--+-------------+---------+------------+ 
|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    | 
+--+-------------+---------+------------+    

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 generic 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    | 
+--+-------------+---------+------------+    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

凝望流年 2024-12-04 19:07:09

创建 Lookups 类。然后,对于 Employee 类的 Gender 和 EmployeeType 字段,将它们替换为 Lookups 字段。然后删除枚举注释。

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.

最笨的告白 2024-12-04 19:07:09

我的理解是,您希望仅以自动方式将所有常量/枚举存储在一张表中。

为此,您可以拥有

  • 一些 XML/属性文件,该文件将在服务器启动时加载以读取此数据。
  • 该类将在服务器启动时读取属性/XML 文件并检查查找数据是否存在。
    • 如果没有,那么它将把数据保存在数据库中。
    • 如果有数据,则在数据库上设置一些标志。如果此标志为 true,则启动类将不会在下次服务器启动时再次调用检查。

这样,启动类可以在启动时检查数据,如果不存在则将其插入。

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.

宁愿没拥抱 2024-12-04 19:07:09

也许你可以将它们放在一起,如下所示并坚持它们?

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文