JPAContainer <--> ComboBox mapping

Hi all,
i’m at the first project using Vaadin (that, form, has become addictive :slight_smile:

I must achieve a simple task but i cant find a way to solution.

I have a JPAContainer for this model (simplified):

class User {

  @Id
  String id;

  String name;

  ... setters and getters ...
}
[/code]And my goal is to use a ComboBox to make use select an entity, autocompleting on 'name' property. So my code looks like:

[code]
ComboBox selector = new ComboBox();

selector.setContainerDatasource(jpaContainer);

selector.setItemCaptionMode(ItemCaptionMode.PROPERTY);
selector.setItemCaptionPropertyId("name");
[/code]
The ComboBox correctly show the "name" property, but, when i need to get the event value, like this:

[code
]@Override public void valueChange(Property.ValueChangeEvent event) {

    User selectedUser = (User) event.getProperty().getValue();

… i get a ClassCastException for the String, aka the name selected.

So, i’ve implemented a Converter, and in this method:


@Override
public Importer convertToModel(String value, Class<? extends User> targetType, Locale locale) throws ConversionException {

    EntityItem<User> item = Selector.this.mContainer.getItem(value);

            return item.getEntity();
        }

I want try to get the selected Entity from the Container.

But now, i need to get Item from container using the “name” properties, and not the ID.

My question:
is this the right approach to my needs? Where i go wrong?

Thank you.

sdsds

Nevermind, solution:

importer = mSelector.getContainer().getItem(value).getEntity();

The mapping between JPAContainer and ComboBox (and other selects) is always bit problematic. In databinding (with BeanFieldGroup) you are using entities as field values and JPAContainer uses database identifiers (container item id == value of the ComboBox). Becauses of this and couple of other reasons,
I don’t recommend to use JPAContainer at all
.

cheers,
matti

Thank you Matti.

I was totally engaged in writing JPAContainer implementations for all my data, also because searching google for “vaadin jpa”, as you can see, is fullfilled with JPAContainer tutorials, so everybody will think that this is the “right way” to bind jpa data models.

Bye.

Hi, I understand. The google presense of JPAContainer is way too good, that is why I just published the rant about it. If you have already invested a lot to JPAContainer, I think it is not necessary sane to throw it all away, but gradually move to a better architecture.

cheers,
matti