ComboBox, SQLContainers and Foreign Keys

Hi,

I’m new to Vaadin and newish to Java. I’m struggling with the creation of a Combobox that utilises SQLContainers for its data source. I have two SQL tables (products and categories). The products table references the categories table using a category_id. I’m therefore adding a reference between the containers.

    SQLContainer productContainer = null;
    TableQuery q1 = new TableQuery("products", DBConnection.getInstance().getConnectionPool());
    q1.setVersionColumn("version");
    try {
          productContainer = new SQLContainer(q1);
          } catch (SQLException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
          }
  
    SQLContainer categoryContainer = null;
    TableQuery q2 = new TableQuery("categories", DBConnection.getInstance().getConnectionPool());
    q2.setVersionColumn("version");
    try {
          categoryContainer = new SQLContainer(q2);
          } catch (SQLException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
          }

    categoryContainer.addReference(productContainer, "category_id", "category_id");

I then create the combobox as follows:

          ComboBox categoryField = new ComboBox("Select product category");
          categoryField.setContainerDataSource(categoryContainer);
          categoryField.setInputPrompt("No category selected");
          categoryField.setItemCaptionPropertyId("title");
          categoryField.setItemCaptionMode(ItemCaptionMode.PROPERTY);
          categoryField.setRequired(true);
          categoryField.setWidth(100.0f, Unit.PERCENTAGE);
          categoryField.setFilteringMode(FilteringMode.CONTAINS);
          categoryField.setImmediate(true);
          fieldGroup.bind(categoryField, "category_id");
          categoryField.setNullSelectionAllowed(false);
          addComponent(categoryField);
  
         
          categoryField.addValueChangeListener(new Property.ValueChangeListener() {
           @Override
           public void valueChange(Property.ValueChangeEvent event) {
                 categoryField.getValue();
                 Notification sample = new Notification("Notification message",String.valueOf(categoryField.getValue()));
                 sample.show(Page.getCurrent());
           }

I have two issues.

Firstly, I want to set the initial state of the combobox to reflect the existing value in the products table (i.e. use the value of category_id in table products and reference the text representation of that from column ‘title’ in categories table). With the code above, when the UI loads, the combobox is empty, but the changelistener is triggered immediately and shows the correct category_id is set – however the text representation of that is not displayed. Other than that, the combobox appears to work – it allows selection from the list of categories retrieved from the SQLContainer. Does anyone know why the text string representing the category_id is not displaying?

Secondly, if I manually select a value in the combobox and try to update the backend SQL, I get the following error:

Unable to convert value of type com.vaadin.data.util.sqlcontainer.RowId to model type class java.lang.Integer. No converter is set and the types are not compatible.

Can anyone provide an example of a converter and how I would implement such for this application?

Kind regards.

Hi,

I’m just wondering (and hoping) that someone may be able to help with the issue described above. I’m still tearing my hair out trying to resolve. Focussing on my first point, I can get a basic combobox working without any issues. The challenge here is populating the field via a reference to a second SQL table (taking an ID field as a foreign key into a second table). The value that is obtained via getValue is the correct ID, but the text is not populated as expected (it remains blank). However, the selectable drop down values are from the second table. Does anyone have any hints why the value is not displaying?

Kind regards.

Hi Charlie. I’m also struggling with ComboBox and some of your difficulties sound familiar to me. What do you mean by “the text representation of that is not displayed”? Do you have a default value selected but it’s text it’s not displayed in the current selected item of the ComboBox? Or you mean the dropdown show a list of items which you can select and you get their value but are displayed as list of blank/empty elements?

After making a selection, the current selected item appears as blank? Do you see a blinking cursor there? And if you click anywhere on the screen outside it, does this change?