Enum Set JPA 2.0

Please take a look at my previous question to get the background.

Enum list versus boolean class

However, my question is how enum arrays work with JPA 2.0 and databases. Since I would probably save the values ​​as strings (because I don't want to risk losing the match), an enumeration of, say,> 20 enumerations in a set can be a very inefficient way to store data compared to the bool class.

Consider a user table with various 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 wishes

0
source share
2 answers

The above database design is not how you should store roles for the user. This is not standardized (since you have multiple values ​​in one column), and therefore it is very difficult to query. How could you ask a query to find out all users who have guest or ap roles, for example?

Change the design to something like this:

 user table : id, name, passord role table: id, name user_role join table: user_id, role_id 

In terms of JPA, you will simply have a User object, a Role object, and a ManyToMany association between these objects: the user has 0, 1 or more roles, and the role is held by 0, 1 or more users.

+3
source

If you want to save information in binary form with a relatively small size, I would go for EnumSet , and if you want to be able to query by type, I would select @ElementCollection .

0
source

All Articles