Сonnect DB as grid backend

I’m yet becoming Junior Java programming and now I’m working with Vaadin framework.

It’s tutorial is very specif and I’m experimenting with it but there I got stuck. My target is to make all changes in UI save in PostgreSQL. It will help me in my future project.

So, in order to do it, i’m using Vaadin SQLContainer ans have setup it as are datasource for the grid (see tutorial mentioned before) and grid actually get filled with data from the PostgreSQL.

The problem starts when i have to edit Listeners that are responsible for editing. They all are coded for work with a class not SQLContainer.

Listener code example:

grid.addSelectionListener(event -> { if (event.getSelected().isEmpty()){ form.setVisible(false); } else { Customer customer = (Customer) event.getSelected().iterator().next(); form.setCustomer(customer); } });

Error:

java.lang.ClassCastException: com.vaadin.data.util.sqlcontainer.RowId cannot be cast to ua.bdproject.nameproject.Customer

I would be much grateful for help or advise!

Hi,

as you might notice from the exception message, the error comes from trying to cast a RowId object to a Customer object. The SQLContainer doesn’t know anything about Customers - all it knows are database columns and rows, so to say. Therefore, the grid selection is a Set of RowId objects, which represent individual rows in the query result set. Take a look in
the Book of Vaadin for information on how to write your SQLContainer’s changes to the database
.

Hope this helps,
Olli