How to use and filter Comboboxes with JPA

Hi everyone and happy new year!

I am still training to use Vaadin. There is 3 comboboxes in my page (to select country then city and then company).
There is COMPANY(table), which includes Address and it includes City and Country etc. In one country can have n cities and in one city can have n addresses.

I am using JPAContainer and JPAContainerCustomField. First user choose a country and after that cities combobox should be repopulated. After that user choose a city and Cities combobox should be repopulated and at last user choose a company and remember that company has Address.

Could someone give me a link to nice example or tell me what is the wisest way to handle this with JPA and comboboxes.

public CountrySelector() {
    	cityContainer = JPAContainerFactory.make(City.class,
        		BazookaCalendarApplication.PERSISTENCE_UNIT);
    	countryContainer = JPAContainerFactory.make(Country.class,
        		BazookaCalendarApplication.PERSISTENCE_UNIT);
        setCaption("Country");
        
        city.setContainerDataSource(cityContainer);
        city.setItemCaptionPropertyId("city");
        city.setImmediate(true);

        
        
        countryContainer.setApplyFiltersImmediately(false);
       
        country.setContainerDataSource(countryContainer);
        country.setItemCaptionPropertyId("country");
        
      
        
        country.addListener(new Property.ValueChangeListener() {
            public void valueChange(
                    com.vaadin.data.Property.ValueChangeEvent event) {
                /*
                 * Modify the actual value of the custom field.
                 */
                if (country.getValue() == null) {
                    setValue(null, false);
                } else {
                	Country countryEntity = countryContainer
                            .getItem(country.getValue()).getEntity();

Any ideas? Shoud I just use country’s cities (countryEntity.getCities())or is there some another way, filters or should I just use JPQL etc?

Thanks
Sami

Hi,

Using CustomField is the right approach indeed. The value change listener in Countries combo box should filter the JPAContainer in combo box listing cities and then again cities should filter the the last combo box listing companies. The last combo box defines the actual value of you field.

If the standard Vaadin filters don’t fit for your needs (as it might be in this particular case), you should use JPAContainers own API. The most versatile approach is to set QueryModifierDelegate to the container. Then you can modify the queries that JPAContainer makes with std. JPA Criteria API.

If you come up with working solution, please post it here as an example for others.

cheers,
matti

Hi,

Marko has been working with some examples for the manual. This one might help you forward with this case:

http://demo.vaadin.com/book-examples/book/#jpacontainer.filtering.entity

cheers,
matti