2

Is there a simple way to store a set/list of Enum items using JPA (with Hibernate)? There could be no duplicates in the list. I currently try:

public EnumSet<MediaRole.MediaRoleEnum> mediaRoles;

But this returns a null pointer when accessing the field.

Luuk D. Jansen
  • 4,402
  • 8
  • 47
  • 90

1 Answers1

2

Just use:

List<MediaRole.MediaRoleEnum> mediaRoles;
Set<MediaRole.MediaRoleEnum> mediaRoles;

depending on your needs. Check Which Java Type do you use for JPA collections and why?

In the mapping file:

<attributes>
    <element-collection name="mediaRoles"/>
</attributes>

If you use annotations, just use the equivalent one:

@ElementCollection
List<MediaRole.MediaRoleEnum> mediaRoles;
Community
  • 1
  • 1
Butaca
  • 416
  • 4
  • 14
  • I don't want duplicates, and using the list will allow this. Is there an easy way to prevent this from happening if using the above, as using getters and setters for the list as a while is not going to help. – Luuk D. Jansen Jun 01 '12 at 18:49
  • Also, you should update your question regarding the non duplicates. – Butaca Jun 01 '12 at 19:10