Hibernate 中所有枚举类型的一张表
我有一个类 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
创建 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.
我的理解是,您希望仅以自动方式将所有常量/枚举存储在一张表中。
为此,您可以拥有
这样,启动类可以在启动时检查数据,如果不存在则将其插入。
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
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?