File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Best practice of mapping collection of enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Best practice of mapping collection of enum" Watch "Best practice of mapping collection of enum" New topic

Best practice of mapping collection of enum

manuel aldana
Ranch Hand

Joined: Dec 29, 2005
Posts: 308

I am having a list assoziation to enums:

class User{
List<RoleAsEnum> roles;

As such with O/R this would get mapped with a join to two tables because of the 1:n relation. For enum reasons one could also consider to use a third join-table, so enum-entries aren't repeated inside the enum table all the time.

Never the less I would like to avoid joins (enums are stupid types and I don't consider them as entities but rather as table-column).

I thought about writing a custom converter, so the enum is serialized into a comma separated string, but I think such an encoding as kind of bad practice. Is there maybe a JPA + hibernate homegrown solution for mapping the 1:n(asEnum) relation?


aldana software engineering blog & .more
Emanuel Kadziela
Ranch Hand

Joined: Mar 24, 2005
Posts: 187
I always wrap my enums in persistable enum entities and map those, providing an easy way to get the actual enum out of the enum entity wrapper. For example:

The above gives you a simple 2 table bidirectional one-to-many / many-to-one relationship between an enum and an entity which needs a collection of those enums. All you have to do is fill out the usual rest of the bean stuff as well as a way to manage the wrapper correctly (i.e. to get and set the enum itself inside it).
I agree. Here's the link:
subject: Best practice of mapping collection of enum
It's not a secret anymore!