Viritin ElementCollectionField with enum

How to use ElementCollectionField with jpa elementCollection of enum?

I have this field within my jpa entity

@ElementCollection(targetClass = MyEnumType.class) @CollectionTable(name = "MyTable", joinColumns = @JoinColumn(name = "columnId", nullable = false)) @Column(name = "myField", nullable = false) @Enumerated(EnumType.STRING) private Set<MyEnumType> enumTypes; and this in my form

private ElementCollectionField<MyEnumType> enumType;

public static class MyTypeRow {
    EnumSelect<MyEnumType> type = new EnumSelect<MyEnumType>();

protected Component createContent() {
    FormLayout layout = new FormLayout();
    enumType = new ElementCollectionField<>(MyEnumType.class, MyTypeRow.class)
                .withEditorInstantiator(() -> {
                    MyTypeRow row = new MyTypeRow();
                    return row;

I got a java.lang.InstantiationException. I guess that enum are not supported in ElementCollectionField.

Is there another way to achieve this?

Got another way using the SubSetSelector.


Yep, EnumSelect is a single select component, SubSetSelector is for multiselection. By extending that for some enum magic, it might be possible to create EnumMultiSelect, which wouldn’t require you to explicitly define the options.