在休眠中使用枚举值填充数据库表
是否有可能让 Hibernate (3.6) 用给定枚举的值填充数据库表? 我有以下类:
@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.
}
我可以使用这个枚举,而不会出现另一个实体类的任何问题,
@Enumerated(EnumType.STRING)
public Role getRole()
我的问题是,如何自动填充相应的表 ROLE ? 所有底层逻辑和定义都位于 XML 规范中。当然,我可以通过 XSL 从这个规范生成一个 sql 文件,并让 Hibernate 在启动时通过 import.sql 语义导入它......但是有没有更优雅的方法呢?
该表应如下所示:
|RoleID|RoleName |
| 0 |ROLE_USER_FREE|
....
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|
....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须选择一方 - 要么使用
Role
作为枚举,要么作为实体。你试图同时做这两件事,但这只会导致一路上遇到麻烦。如果您想使用枚举
Role
中删除@Entity
注释。它不是一个实体,它没有主键。它也不应该是可变的,因此坚持它没有什么意义。Roles
(或任何名称)表。 Hibernate 按名称(如果您使用@Enumerated(EnumType.STRING)
映射)或按values()
数组中的索引(如果您使用>@Enumerated(EnumType.ORDINAL)
注释)。无论哪种方式,它都不会引用您的附加表。通过映射 (@Enumerated(EnumType.STRING)
),一开始就有RoleID
是没有意义的。如果你想使用一个实体
Role
成为一个真正的实体——带有getters/setters和标识符的POJO。它可能是可变的,也可能不是可变的,具体取决于您想要什么。@ManyToOne
。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
@Entity
annotation fromRole
. 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.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 invalues()
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 haveRoleID
to begin with.If you want to use an entity
Role
a true entity - POJO with getters / setters and identifier. It may or may not be mutable depending on what you want.@ManyToOne
from your other tables.这很好地填充了我的数据库
This populates my database fine
您可以通过映射到“枚举表”来实现数据库中角色列中的“数字 ID”。要考虑 postgresql 本机枚举类型及其与其他方法的比较,请务必阅读 https://vladmihalcea.com/the-best-way-to-map-an-enum-type-with-jpa-and-hibernate/
我会建议采用序数方式插入方便的“可能的枚举值表”:
使用枚举所需的实体:
比在单元测试中确保序数始终相同,并且在项目的模式迁移部分中将枚举插入数据库以使数据库人员使用它们可以实现良好的连接:
相对于 STRING 方法的优点:
缺点:
You can achieve having "numeric id" in role column in database with mapping to "enum table". To consider postgresql native enum type and its comparison to other approaches, be sure to read https://vladmihalcea.com/the-best-way-to-map-an-enum-type-with-jpa-and-hibernate/
I would suggest to go ordinal way with inserting convient "possible enum values table":
Desired entity using enum:
Than in unit test ensure that ordinals are always same and also in schema migration part of you project insert enums to database to make them use by database guy to have nice joins possible:
Benefits over STRING approach:
@Column(columnDefinition = "smallint")
to controll data type used)Downsides: