bind entity to form

Going through docs I learned, there are several ways to bind the data object (entity bean) to the form.

  1. wrap with BeanItem & assign
    myForm.setItemDataSource(new BeanItem(bean))
  2. wrap with BeanItem & use binder
    new FieldGroupBinder(new BeanItem(bean)).bindMemberFields(myForm)

while an all examples I found yet another method is used:
3. myForm.setEntity(bean)

how 3 differs from 1 and 2 ?
cheers
Maciej

Hi, I’m not aware of any “setEntity” method in Vaadin Framework’s API. Sounds like just a helper method coded in the examples themselves to encapsulate some logic related to data binding. But I don’t really know what examples are you talking about. Maybe if you post the links to the docs you are refering to, I could have a look at them.

@Inject // With Vaadin CDI one can also inject basic ui components
PhoneBookEntryForm form;

then in the code:
form.setEntity(entry);

in your JPA example
https://github.com/mstahv/jpa-addressbook/blob/master/src/main/java/org/example/MainView.java

There is actually 4th way to do this,

  1. use BeanFieldGroup
    final BeanFieldGroup binder = new BeanFieldGroup(beanclass);
    binder.setItemDataSource(new BeanItem(bean));
    binder.bindMemberFields(frmUser);

so what is the difference between 1, 2, 3, 4 ?

also, how to deal with forms created with designer, I can’t add @PropertyId for the fields in the form.
Shall I inherit a class and annotate the getters?

Ok, thanks. That
setEntity
method is defined in the Viritin add-on which I have no experience with. As a short anwser I’d say that the different ways are just convenient APIs on top of the
Vaadin Data Model
which is based on Properties, Items, and Containers. So, if you study and understand the model it should help you to understand when to use each aproach.

Alejandro, you really should get some experience with Viritin as well. Seriously.

AbstractForm in Viritin (the “setEntity” option) uses an enhanced BeanFieldGroup behind the scenes. This way you don’t need to “think in Container-Item-Property” at all, but just bind your entities/beans to your UI components. The closest thing with “vanilla Vaadin” is:

BeanFieldGroup.bindFieldsUnbuffered(bean, objectWithMemberFields); This is most of the time the thing you wan to do, instead of manually wrapping your beans into BeanItems or using FieldGroup directly. It is really rare situation when using Item/Property API brings any value for you, infact, I don’t even remember when I would have used them in my real world Vaadin apps.

cheers,
matti

Hi Matti! BeanFieldGroup.bindFieldsUnbuffered(bean, objectWithMemberFields), bind Entity to Form and the opposite? How to bind Form to Entity, is there a command like bindFieldsUnbuffered?