@ElementCollection of type enum



I have in an entity called User with the following field: @ElementCollection(fetch = FetchType.EAGER) private Set<UserPermission> permissions;

package x.x.x.x

public enum UserPermission {

Use case: there are users which contain the first 2 permissions (BASIC and COACH, so the first 2 entries).

Bug: If UserPermission enum is modified by adding, for example, BASIC231 between BASIC and COACH values, data in ObjectDB will be altered in a way that the users that previously had BASIC and COACH as permissions, now they would have BASIC and BASIC231.

Conclusion: @ElementCollection(fetch = FetchType.EAGER) private Set<UserPermission> permissions; saves enum values as ordinal, not String, therefore any modification of the order alters all entities in the DB.

Mention: I have seen this bug by checking the values in DB explorer, but also by printing logs directly in the code.

Desired case: @ElementCollection(fetch = FetchType.EAGER) containing enums should be order independent.



By default, enum values are stored as ordinal values. This is the default in JPA, and it is also more efficient, since numbers are more efficient than strings (take less storage space and faster to save and load).

You can easily solve the problem by adding new enum values only at the end of the list.

Alternatively, if you prefer using enum values as strings, try annotating the collection with @Enumerated(STRING)javax.persistence.EnumeratedJPA annotationSpecifies that a persistent property or field should be persisted as a enumerated type.See JavaDoc Reference Page....

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

See also this section in the ObjectDB manual about using enum.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)


I've already tried to use @Enumerated(STRING) on the @ElementCollection, but it didn't have any effect. It would have been nice to work.

I know how enums work, don't worry.

So, we just have to live with this limitation? 


Try the following test case:

import java.util.*;

import javax.persistence.*;

public class T1319 {

    public static void main(String[] args) {
        EntityManagerFactory emf =
        EntityManager em = emf.createEntityManager();
        MyEntity entity = new MyEntity();
        entity.list = new ArrayList<MyEnum>();
        em = emf.createEntityManager();
        TypedQuery<MyEntity> query =
            em.createQuery("SELECT e FROM MyEntity e", MyEntity.class);
        List<MyEntity> resultList = query.getResultList();
        for (MyEntity e : resultList) {

    public static class MyEntity {
        @Id @GeneratedValue Long id;
        @ElementCollection @Enumerated(EnumType.STRING)
        ArrayList<MyEnum> list;
        public String toString() {
            return "MyEntity#" + list;
    public static enum MyEnum {
        V1, V2, V3;

You can run it several times, adding and removing values in MyEnum, and see that entity objects preserve their enum values (but values are changed when EnumType.STRING is not specified).

If you have a different scenario in which it doesn't work, please demonstrate it by changing this test case.

In general, you should always demonstrate such issues using a test case, as explained the posting instructions.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Your test seems to work all the time. Thank you and I'm happy that it works.

It's strange then why it didn't when I tried in my project. Maybe I didn't pay enough attention nor I cleaned the project before redeploying the war. Sorry for trouble.

Thank you!

Post Reply

To post a reply and/or subscribe to update notifications - please login