Vaadin - 如何绑定POJO列表

时间:2013-01-23 08:24:31

标签: list data-binding combobox vaadin pojo

在Vaadin 7中,我试图将我的电话号码组合框绑定到嵌入POJO的POJO列表中。 例如:我有包含List手机的PersonVo;和PersonCommunicationVo包含电话号码的值。

我用来绑定PersonVo的其他属性的是:

BeanFieldGroup<PersonVo>personBinder = new BeanFieldGroup<PersonVo>(PersonVo.class);
personBinder.setItemDataSource(person);
myFormLayout.addComponent(personBinder.buildAndBind("Firstname", "firstName"));
myFormLayout.addComponent(personBinder.buildAndBind("Country", "country.name"));

这里是PersonVo:

@Table(name = "CONTACT.T_PERSON")
@Entity
@Audited
public class PersonVo {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id",updatable = false)
    private Long personId;

    @Column(name = "FIRSTNAME")
    private String firstName;

    @Column(name = "LASTNAME")
    private String lastName;

    @Column(name = "TITLE")
    private String title;

    ...

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "personId")
    @AuditMappedBy(mappedBy = "personId")
    private List<PersonCommunicationVo> **phoneNumbers**;

    ...

和PersonCommunicationVo:

@Entity
@Table(name = "CONTACT.PERSON_COMMUNICATION")
@Audited

public class PersonCommunicationVo {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id",updatable = false)
    private Long persCommId;

    @Column(name = "PERSON_ID")
    private Long personId;


    @ManyToOne
    @JoinColumn(name = "COM_TYPE_ID")
    private CommunicationTypesVo communicationType;

    @Column(name = "VALUE")
    private String **value**;

我想要做的是根据PersonCommunicationVo中的,将我的电话号码组合框绑定到PersonVo的列表 phoneNumbers 。 但困难在于根据组合框中选择的内容在列表中到达正确的PersonCommunicationVo。例如:我在组合框中选择了电话号码+32 123 456 789,该值应映射到phoneNumbers.get(indexOfSelectedItem).getValue()。 要更新电话号码,请添加新电话号码......

感谢您的帮助!

2 个答案:

答案 0 :(得分:-1)

也许我不理解您的数据模型,如果是这样,请您发布PersonVo,PhoneVo(?)和PersonCommunicationVo并进一步解释它为什么要像您一样保存它。为什么你的personvo包含手机列表?我之前从未使用过beanfieldgroup,但我想到的是:

ComboBox cb = new ComboBox("Phones", person.getPhones())
personBinder.bind(cb, "phones");
myFormLayout.addComponent(cb);

答案 1 :(得分:-2)

你可以尝试这样的东西(java 1.7语法和Vaadin7):

...
    FieldGroup fGroup = new FieldGroup( new BeanItem<PersonVo> (personInstance) );
...
    BeanItemContainer<PersonCommunicationVo> numbers = new BeanItemContainer<>( PersonCommunicationVo.class );
    numbers.addAll( personInstance.getPhoneNumbers() );

    ComboBox phoneCombo = new ComboBox( "Phone Numbers", numbers );
    phoneCombo.setItemCaptionMode( ItemCaptionMode.PROPERTY );
    phoneCombo.setItemCaptionPropertyId( "**value**" );

    fGroup.bind (phoneCombo, "**phoneNumbers**")

    myFormLayout.addComponent(phoneCombo);
...