(抱歉,这是一个新问题)
我有两个JPAContainer,一个用于Employee类/表,一个用于语言类/表。员工可以选择一种语言。
我正在尝试创建一个带有用于语言选择的组合框的Employee编辑表单,并使用FieldGroup来处理绑定和创建。
我可以通过JPAContainer下拉列表中包含Languages表中的条目,但是它不会显示Employee记录中已经存在的值。我究竟做错了什么?
//Key parts of code....
private JPAContainer<Employee> employees;
private JPAContainer<Language> languages;
FormLayout formLayout = new FormLayout();
formFieldGroup = new FieldGroup(employeeItem);
Field<?> firstName = formFieldGroup.buildAndBind("First name", "firstName");
Field<?> lastName = formFieldGroup.buildAndBind("Last name", "lastName");
Field<?> userName = formFieldGroup.buildAndBind("Username", "userName");
Field<?> fullName = formFieldGroup.buildAndBind("Full name", "fullName");
Field<?> initials = formFieldGroup.buildAndBind("Initials", "initials");
ComboBox language = new ComboBox("Language",languages);
language.setContainerDataSource(languages);
language.setItemCaptionPropertyId("languageName");
language.setFilteringMode(FilteringMode.CONTAINS);
language.setImmediate(true);
formFieldGroup.bind(language, "language");
firstName.addValidator(new BeanValidator(Employee.class, "firstName"));
lastName.addValidator(new BeanValidator(Employee.class, "lastName"));
firstName.addValidator(new BeanValidator(Employee.class, "userName"));
lastName.addValidator(new BeanValidator(Employee.class, "fullName"));
lastName.addValidator(new BeanValidator(Employee.class, "initials"));
formLayout.addComponent(userName);
formLayout.addComponent(fullName);
formLayout.addComponent(firstName);
formLayout.addComponent(lastName);
formLayout.addComponent(initials);
formLayout.addComponent(language);
// A few details of the domain classes
@Entity
@Table(name="Employees")
public class Employee extends BaseEntity
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="EmployeeID")
private Integer employeeID;
...
@ManyToOne
@JoinColumn(name="LanguageID",updatable=false)
private Language language;
...}
@Entity
@Table(name="Languages")
public class Language extends BaseEntity
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="LanguageID")
private Integer languageID;
@Column(name="LangaugeName")
private String languageName;
... }
最佳答案
我在Vaadin论坛上回答了这个问题,我们收到了以下回复(感谢Jonatan Kronqvist)。
迈克尔,你好
JPAContainer项目ID与数据库中的ID相同,
表示在选择/组合框中使用JPAContainer会导致该值
select是数据库中行的ID,而不是POJO
雇员实体POJO所期望的。添加一个
SingleSelectConverter到您的组合框应该解决您的问题:
language.setConverter(new SingleSelectConverter(language));
...
HTH,/乔纳丹