HbnContainer + Select

Hi,
I recently joined Vaadin to check out the possibilities of the framework.

I encountered a little problem. I am using a MySQL database, which I connect to using HbnContainer. So far, everything was very good, but I now have a problem with a Select component. Here are some details :
I have a table called “Reservation” which is used as a relation between a table “User” and a table “Restaurant”. A Select item is bound to each of these table. It works to display the values, but not to select them. I tried a couple of solutions, but nothing worked :

  • redefining the setValue() method of the select :
    Restaurant r = (Restaurant) session.getSession().get(Restaurant.class, (Serializable)newValue);
    super.setValue(r.getId(), repaintNeed);
    The problem here is that it seems to build a new Restaurant instead of using the one in DB.

  • trying to get the restaurant from the container, using a local field of the FieldFactory. Same problem.

  • a lot of other solutions which lead to incorrect type exceptions

I already did some researches and found that using HbnContainer may not be a good idea, but this is the only one I found to use persistance using Hibernate.

Is there some (easy) way to do that ?

Thanks.

The value (selection) of a Vaadin selection component is the item identifier in the container - probably an Integer or a Long in your case. On the other hand, in your Reservation entity class, you probably have a reference to a Restaurant instance. You need to make conversions to both directions for this to work smoothly.

Unfortunately, this is not as easy as it should be. The best way at the moment is probably to use wrappers around your fields that perform the conversions. This can be done with FieldWrapper from the
CustomField add-on
.

When working on the Vaadin Roo addon, I have written such wrappers for JPAContainer, they should be easy to modify to work with HbnContainer. The current versions might not be as clean code as they could be, but they seem to work. Here are templates for the
many-to-one
and
many-to-many
wrappers that are based on FieldWrapper. You can then wrap your fields in your field factory.

I remember Matti also wrote a subclass of ComboBox that supported the conversions specifically for many-to-one relations with HbnContainer, but I don’t know where it is.

Better conversion support should definitely be in the core, but implementing it has been delayed for quite a while because doing it properly would break some of the current APIs - it will come in future versions.