枚举设置JPA 2.0 [英] Enum Set JPA 2.0
问题描述
请查看我之前的问题以获取背景信息.
但是,我的问题是枚举集如何与JPA 2.0和数据库一起使用.由于我可能会将值保存为字符串(因为我不希望失去映射的风险),因此与bool类相比,在集合中使用大于20个枚举的枚举可能是一种非常无效的存储数据的方式. >
考虑一个具有不同角色的用户表:
user | password | role (enum)
adm 1223 admin, guest, www, lab, elevator, ap, // Might be more than > 20
sam 0000 admin
.......
我应该选择什么?你有什么主意吗?
最诚挚的问候
上面的数据库设计不是存储用户角色的方式.它不是规范化的(因为您在单个列中有多个值),因此很难查询.例如,您将如何进行查询以了解所有具有角色guest
或ap
的用户?
将设计更改为类似的内容:
user table : id, name, passord
role table: id, name
user_role join table: user_id, role_id
按照JPA的术语,您将只具有一个User
实体,一个Role
实体以及这些实体之间的ManyToMany
关联:用户具有0、1个或多个角色,并且一个角色由0持有. ,1个或多个用户.
Please take a look at my former question to get the background.
List of enum vs. class of booleans
However, my question is how enum sets are working with JPA 2.0 and databases. Since I would probably save the values as strings (cause I don't want risk to loose the mapping), a enumset of say > 20 enums in the set might be a very ineffective way to store data comparing with the bool class.
Consider a user table with different roles:
user | password | role (enum)
adm 1223 admin, guest, www, lab, elevator, ap, // Might be more than > 20
sam 0000 admin
.......
What should I choose? Do you have any ideas?
Best regards
The above database design is not how you should store roles for a user. It is not normalized (since you have several values in a single column), and is thus very hard to query. How would you make a query to know all the users which have the roles guest
or ap
, for example?
Change the design to something like that:
user table : id, name, passord
role table: id, name
user_role join table: user_id, role_id
In JPA terms, you would simply have a User
entity, a Role
entity, and a ManyToMany
association between those entities: a user has 0, 1 or several roles, and a role is held by 0, 1 or several users.
这篇关于枚举设置JPA 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!