How to do form mapping id <--> bean

Hi,

I have several beans that should be edited in a form. These beans have lots of ID properties in them. These IDs represent other beans. These other beans should be selectable via a NativeSelect and after commiting the data, the ID of the selected bean should be placed in the “form” bean. Here’s a little code example:

public class FormBean
{
  Long id;
  String name;
  Long roleId;  // id of a RoleBean
  // getter and setter removed for simplicity
}

public class RoleBean
{
  Long id;
  String roleName;
  // getter and setter removed for simplicity
}

In this example, the FormBean should be edited in the form. The “Role” should be selectable via a NativeSelect that shows a list of RoleBean. When the form is commited, I get a conversion exception.

How can I tell the Form or NativeSelect to use the id of RoleBean?

Best regards

Andreas

The
CustomField add-on
contains some helper classes for such mappings - see BeanFieldWrapper, BeanSetFieldWrapper and the converters they use.

In the future, with Vaadin 7, such conversions should be easier to achieve.

Henri,

thank you for your add-on. But I think I haven’t understood it right. I get a
com.vaadin.data.Property$ConversionException: java.lang.NoSuchMethodException: java.lang.Long.getId()
exception on commit. I used the BeanFieldWrapper as follows:

    RoleContainer rc = new RoleContainer(roleList);
    NativeSelect roleNS = new NativeSelect("Available roles", rc);
    roleNS.setItemCaptionPropertyId("roleName");
    Field roleField = new BeanFieldWrapper<RoleBean>(roleNS, RoleBean.class, rc, "id");

The
roleField
is returned in createField() for the property
roleId
of FormBean.

Please point me to the place where my mistake is.

Thanks in advance

Andreas

Hi Henri,

sorry to bother you again, but I couldn’t find a way to get the BeanFieldWrapper/BeanSetFieldWrapper working in my scenario.

Could you please tell me how to use them in my case? I tried several possibilities, but none of them worked…

Thanks

Andreas

Hi Henri,

do you have a minute and would you be so kind to answer my question?

Thanks

Andreas

Maybe asking the obvious, but does your FormBean have getId()?

Hi Johannes,

yes, FormBean has getId(). My problem is, that getId() is called on
Long
! As I already wrote: I think I’m using the BeanFieldWrapper the wrong way and I don’t know how to use it right…

Sorry I misread what you had wrote. My guess is that the BeanFieldWrapper expects instance of RoleBean instead of id of role bean as the field value. You have to use a property converter there. See BeanFieldPropertyConverter.

Hi,

Vaadin 6.7.3 has a small but powerful change to help this kind of issues. Form now sets the property to existing property in the Field if it implements Property.Viewer interface. This allows you to write “converter properties” and configure them in e.g. FormFieldFactory. The JPAContainer 2.0.0-RC1 release already heavily relies on this tiny little improvement. It contains a field factory that automatically generates appropriate selects and multiselects for referenced entities. If your beans are JPA annotated, it might be the easiest option for you.

Ticket about the change is here:

http://dev.vaadin.com/ticket/7942

There also a small helper class called PropertyTranslator in the Directory. It was actually finally bundled into JPAContainer too.

http://vaadin.com/directory#addon/propertytranslator

JPAContainer “pro page”:

https://vaadin.com/add-ons/jpacontainer

cheers,
matti

@Johannes: I tried the BeanFieldPropertyConverter but I had still no luck. Since I learn best from examples: do you have one at hand?

@Matti: I tried also the PropertyTranslator and I also had no luck with that addon. The example is very minimalistic and I don’t know how to provide the right value/class/instance for
beanItemProperty
. I would be glad, if you could show an example with a bit more source code.

@Both: thanks for your help so far. My next answer/post will be at the earliest next year. So have a merry christmas and a happy new year.

Andreas