gpt4 book ai didi

java - 带有 JPAContainer 和 FieldGroup 的 Vaadin 7.0.1 组合框

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:34:18 26 4
gpt4 key购买 nike

(对不起,如果这是一个新问题)

我有两个 JPAContainer,一个用于 Employee 类/表,一个用于 Language 类/表。员工可以选择语言。

我正在尝试创建一个员工编辑表单,其中包含用于语言选择的组合框和用于处理绑定(bind)和创建的 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)收到了以下回复。

Hi Michael,

JPAContainer item IDs are the same as the ID in the database, which means that using a JPAContainer in a select/combo box causes the value of the select to be the ID of the row in the database and not the POJO itself, as is expected by your Employee entity POJO. Adding a SingleSelectConverter to your combo box should fix your problem:

language.setConverter(new SingleSelectConverter(language));

...

HTH, /Jonatan

关于java - 带有 JPAContainer 和 FieldGroup 的 Vaadin 7.0.1 组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15251275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com