Storing EnumSet in a database?

前端 未结 10 1848
借酒劲吻你
借酒劲吻你 2020-12-03 21:24

So in C++/C# you can create flags enums to hold multiple values, and storing a single meaningful integer in the database is, of course, trivial.

In Java you have Enu

10条回答
  •  旧时难觅i
    2020-12-03 21:32

    Without going into the debate about pros and cons of ordinal values in the database - I posted a possible answer to the given question here: JPA map collection of Enums

    The idea is to create a new PersistentEnumSet which uses the implementation of java.util.RegularEnumSet, but offers the elements bitmask to JPA.

    That one can than be used in an embeddable:

    @Embeddable
    public class InterestsSet extends PersistentEnumSet {
      public InterestsSet() {
        super(InterestsEnum.class);
      }
    }
    

    And that set is used in the entity:

    @Entity
    public class MyEntity {
      // ...
      @Embedded
      private InterestsSet interests = new InterestsSet();
    }
    

    For further comments see my answer over there.

提交回复
热议问题