If you need ComboBox
that give you as a value Language
you could extend ComboBox class:
class LanguageComboBox extends ComboBox {
public LanguageComboBox(String caption, Collection<Language> languages) {
super(caption, getShortcuts(languages));
}
@Override
public Language getValue() {
return new Language((String) super.getValue());
}
private static List<String> getShortcuts(Collection<Language> languages) {
// extract shortcuts using java 1.8
return languages.stream()
.map(language -> language.getShortcut())
.collect(Collectors.toList());
}
}
and then you can easily add this to FieldGroup
with BeanItem<Customer>
:
ComboBox comboBox = new LanguageComboBox("Language", LANGUAGES);
fieldGroup.bind(comboBox,"language");
After submit your customer will have the proper value of language.
In my opinion you shouldn't use entities on the view - as you mentioned you cannot change Customer
object and probably this class contains much more fields that you need to your view. I would rather create separate POJO class to this view - this give you much more flexibility and there will be no such problems like you reported. Of course on the end you need to convert your view object to entity or even more entities but in most cases it is worth to bear that cost.