Best way to load data from SQL into ComboBox

Yes, I have a similar question out there, but today as I was thinking about it I thought of a more general question: What is the best way to load the following data set into a ComboBox ( Vaadin 7 for now, but feel free to provide Vaadin 8+ example if you like ):
select skusku 'ID Column', concat( skusku, ' - ', skudsc ) 'Description Column' from sku where ...
Basically, the “ID Column” will be the ComboBox value and the “Description Column” is the item caption. I don’t have to use SQLContainer ( especially since it seems Vaadin 8+ got rid of it ), I just need to know the best way to handle it. None of the examples for Vaadin 7 even tell you how to choose an arbitrary column as the “value” for the ComboBox ( they assume all tables will have an “ID” column, it seems ), let alone how to do something like what I am describing.

Heck, even good examples of using [FreeformQuery]
(http://https://vaadin.com/download/release/7.7/7.7.13/docs/api/com/vaadin/data/util/sqlcontainer/query/FreeformQuery.html) would help.

Also, is it my imagination, or did Vaadin 8 remove these handy features?

It is not just ComboBox, but Grid, forms and many other cases.

The best practice would be to have repository class, a service that handles all the database functions. I.e. that should implement methods like getAllItems, etc. usually returning list or collection of items, i.e. beans which you query from the database. Then it is straightforward to create e.g. a new BeanItemContainer (Vaadin 7) or ListDataProvider (Vaadin 8) and setup the ComboBox with that as a data source.

What kind of database access you should use in the repository then. That depends on the complexity of the application. Nowadays there is quite a lot of tools to make the setup easier (say Spring Data, DeltaSpike or JOOQ), so in many cases you do not need to write the boiler plate code yourself.

You can download sourcecode of the Vaadin 8 version of our demo application from vaadin.com/start, it has a version for Spring and Java EE based tech stacks.

And this is the best way if we just want to READ data ( thus my title has word “load” )? Seems like a lot of overhead when all I want to do is load some data into a ComboBox. In other words, I just need “select”, don’t need “delete”, “update”, or “add”.

I am still looking at the StarterApp, but it is an uphill battle. My current website talks to a backend service using XML, so did not have to worry about this. But we are trying to speed things up, and we think calling SQL directly for things like dropdowns will help. Do you think this would help?

Current application dropdown loading:

  1. Ask backend service for data needed in ComboBox ( XML sends a list of EntryType elements )
  2. Get XML back from service, loading it EntryType by EntryType into ComboBox using combo.addItem(entry.getIdent().trim()) and combo.setItemCaption( entry.getIdent().trim(), entry.getDescription().trim() )

We think it is either the XML handling or the loading of the ComboBox that is causing our performance issues, and so theorize that using SQL would speed things up. The backend service already uses SQL and tends to be relatively quick. Is our theory reasonable or off basis, if you had to make an educated guess?

Sort of new to this aspect of things ( direct DB access from a Vaadin application ). As I said a couple days ago, we have been working through a web service, which took care of the DB access for us. Anyway, the demo application, while interesting, does not help me because my biggest problem is not the actual DB access ( I can deal with that with the connection pool, which is already in my code for Jasper Reports integration ). My confusion is mapping a table to a bean. You mentioned Spring Data, DeltaSpike or JOOQ. I assume Hibernate could be added to your list, correct? If I am new to this, does anyone have a suggestion?

Yes, sort of not related to Vaadin, but thought I would ask.

well, unless I am mistaken, well, unless I am mistaken, [this]
(https://vaadin.com/blog/vaadin-jooq-implementing-business-web-applications-in-plain-java) is a good start on jooq. I do need to figure out how to map to my app, which is non-spring, but should be a good start, and seems handy.

You say to use BeanItemContainer for ComboBox, but how does ComboBox know which “column” is the ID field and which one is the caption? All the examples I see online assume there is literally an “id” data element.

Maybe the only way to do it is to go through the result set manually and add one by one, just asking to make sure.

With Vaadin 7 I would recommend to check method comboBox.setItemCaptionPropertyId(…) for how to define the property that is used in ComboBox for the captions. Note when you use ComboBox with list of beans in BeanItemContainer and mentioned method, the selected value of the ComboBox is the bean (i.e. you get itemId, which in case of BeanItemContainer is the bean itself). Also there is method comboBox.setItemCaptionMode(…) which might be useful depending on your application. Now, if you want the ComboBox to return String instead of the bean, then you need to traverse your result from database query (i.e. the list of beans) and collect the String values from the beans that you want to use in ComboBox into list of strings and for data source for the ComboBox from these.