Why binding a bean to form fields invokes multiple bean accessor calls?

I have a custom login form and I have bound the fields to my custom bean like so:

class RegistrationForm extends Form {
....
        setImmediate(true);
        //setWriteThrough(true);

        email.setRequired(true);
        email.setRequiredError("Please enter email");

        password.setRequired(true);
        password.setRequiredError("Please enter password");

        addField("email", email);
        addField("password", password);
        ...
        BeanItem<LoginBean> item = new BeanItem<LoginBean>(bean);
        email.setPropertyDataSource(item.getItemProperty("email"));
        email.addValidator(new EmailValidator("Email is invalid"));
        password.setPropertyDataSource(item.getItemProperty("password"));
...

} 

Now, as I tab through the fields, the bean methods are called.


The problem is that each time I tab from password to the email field the bean accessor methods are called multiple time.

Output:
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:set pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v
com.gsl.web.tests.vaadin.LoginBean@136f182loginbean:get pwd:v


The get password method of the bean is getting called 8 time!!!


Is this a bug or is there some logical explanation on why the beans methods are called so many time?

It will be much appreciated if someone from Vaadin team can address this.
This is a critical issue
. If bean getters are called 8 times, this can have serious impact in cases when getters are doing any complex processing, such as retrieving data from a database.

[b]

Why are getters called 8 times?

  1. on form submit or
  2. when tabbing to another field[when set immediate is true]

[/b]

The Property interface is used to fetch the value whenever it is needed instead passing a cached copy around. The calls you are seeing come from setting the internal value of the field, painting the field, checking if it is empty, validator execution etc. which each ask the property for its value.

In case your getters are expensive, you could use a wrapper around your bean that caches the value transiently. Also with most high level database libraries etc., the value, once fetched to the entity, is not re-fetched by the getter.

Alternatively, especially if you are only using BeanItem and creating its instances explicitly, it should not be hard to modify BeanItem to use a custom Property implementation with a cache instead of MethodProperty - the hardest part is deciding when to discard the cache and re-fetch the value.

Thanks for the response Henri. You have been most helpful.