Reusable BeanItem

Is there a reason why the BeanItem class cannot be reused between different bean instances (other than I haven’t submitted a patch yet, that is)?

I would expect something like this:


BeanItem item = new BeanItem(MyBean.class);
item.setBean(myBean1);
...
item.setBean(myBean2);

Thought I’d bump before actually starting to implement this…

Suggested patch provided here:
http://dev.vaadin.com/ticket/4302

Hi Mattias!

We usually never reuse Item instances in the projects we do here at IT Mill, and I have yet to see a use-case for this in the projects I’ve worked on. This is perhaps something of a best-practice; items should not change, since you have a unique item ID identifying said item. If the actual item changes, you might have a hard time following the logic of your code, since now the unique ID is pointing to a completely different object (behind the item).

Purely technically there’s no reason for you to not be able to do what you suggest, but the result could be hard to follow.

Perhaps the person who actually designed the BeanItem could chime in with his thoughts?

HTH,
/Jonatan

Hmmm. My use case at hand is this:
The project we are looking into migrating to Vaadin consists of a lot of “registry update” pages. For example an article registry with a key field (article no.) and lots of attributes. When the user changes the value of the key field the attributes should be updated correspondingly (page reload with non-AJAX) or possibly cleared for new records (new articles).

I tried recreating the Form each time, but the performance seemed less that satisfactory. The third alternative would be to keep the Form and update the data source Propery of each form Field. I have yet to find a good way to do this semi automatically (as in autocreating a Form given a BeanItem).

I’ve got another use case for the proposed changes: When working with Hibernate a detached instance (from a previous session) can be attached to the current session using the merge() method. This method returns a new reference to the same entity, and at the same time the old reference should be discarded. It’s still valid and usable, but it’s no longer kept up-to-date, e.g. new values of generated properties will not be reflected.

In this case it would be very helpful to have an item.setBean() method to update the BeanItem to use the new reference. All tables, forms and trees showing the item would be automatically updated. Without the method I have to either create a BeanItem for the new instance and replace the old item in all containers and forms, or simply reload the entire container contents from the database.

Regards,
Ingo