(对不起,如果这是一个新问题)
我有两个JPAContainers,一个用于Employee类/表,一个用于Language类/表。员工可以选择一种语言。
我正在尝试创建一个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;
... }
答案 0 :(得分:10)
我通过Vaadin论坛回答了这个问题得到了以下回复(感谢Jonatan Kronqvist)。
嗨,迈克尔,
JPAContainer项目ID与数据库中的ID相同 意味着在select / combo框中使用JPAContainer会导致该值 select的选择是数据库中行的ID而不是POJO 本身,正如您的员工实体POJO所期望的那样。添加一个 SingleSelectConverter到你的组合框应该解决你的问题:
language.setConverter(new SingleSelectConverter(language));
...
HTH,/ Jonatan
答案 1 :(得分:2)
我写了一篇关于这个主题的博文:Select Nested JavaBeans With a Vaadin FieldGroup。在本文中,我将尝试解释Vaadin的FieldGroup如何在内部工作以及它如何将Item
属性绑定到字段组件。在尝试为编辑的Language
选择引用的JavaBean(例如此问题中的Item
)的情况下,了解此属性绑定的工作方式以及如何使用自定义com.vaadin.data.util.converter.Converter
实施。这也解释了迈克尔的答案,为什么在这种特殊情况下需要SingleSelectConverter
。