I have an entity Person.
public class Person {
private Integer id;
private String name;
public Person(Integer id, String name) {
this.id = id;
this.name = name;
}
public String getName() { return name; }
}
And Combo,
ComboBox personComboBox = new ComboBox<>();
layout.addComponent(personComboBox);
List person = new ArrayList<>();
person.add(new Person(8, “Steve Jobs”));
person.add(new Person(99, “Tim Cook”));
personComboBox.setItemCaptionGenerator(Person::getName);
personComboBox.setItems(person);
Now, the combo shows option: “Steve Jobs” and “Tim Cook”. When I select either options, I get the value 8 and 99 respectively. However, when I want to setValue, it requires a Person object. I know the person id and I have to convert the id into and person object. The way to do this it either query the database or loop through the person list object. This is extremely inconvenient and troublesome. Is there a better way to do this.
In my app, Person is store on a database and retreive using JPA. I have simplified the example here.