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>();
}

@Override
protected Component createContent() {
    FormLayout layout = new FormLayout();
    enumType = new ElementCollectionField<>(MyEnumType.class, MyTypeRow.class)
                .withCaption("Types")
                .withEditorInstantiator(() -> {
                    MyTypeRow row = new MyTypeRow();
                    row.type.setOptions(MyEnumType.values());
                    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.

Solved.

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.

cheers,
matti