Vaadin-Grid not displaying the data from the database it is supposed to show

1.8k views Asked by At

I am writing a Vaadin Spring application and have the problem that the data from the database are not showing in the Grid (= a Vaadin 8 UI-Component for displaying tables).

I have a suspicion as to where the error may be located (see my remarks at the end of this post).

Everything else (apart from the data not showing in the Grid) is working fine. The database table was automatically created with the help of Spring JPA annotations, the Grid is displayed nicely in the UI, etc. ... only the data from the database are not showing up in the Grid.

For this question I created a new mini-project. The code for this mini-project you can find below. The ui simply consists of a Grid, which is supposed to display the content of a database. (Since the Grid doesn't display anything, the UI is basically an empty frame, see the last screenshot).

The problem in this mini-project is the same as in the original (and much larger) project: the data from the database is not showing up in the Grid.

The mini-project is called "demo" and was created with the help of SpringInitializr. The selected dependencies are shown in the following screenshot:

enter image description here

My demo-app consists of 3 little packages: model, database_access and ui:

enter image description here

The model consists of a single class called "Person". Here's the code of this file:

package com.example.demo.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Persons")
public class Person {

    @Id
    @Column(name="id")
    private int id;

    @Column(name="name")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

The database-access package consists of 3 files:

PersonRepository, PersonService and PersonServiceImpl

... whereby PersonServiceImpl is the implementation of the PersonService interface.

Here's PersonRepository:

package com.example.demo.database_acess;


import com.example.demo.model.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.stream.Stream;

@Repository
public interface PersonRepository extends JpaRepository<Person, Integer> {

    @Query("SELECT p FROM Person p")
    Stream<Person> findAllByCustomQueryAndStream();
}

Here's PersonService:

package com.example.demo.database_acess;


import com.example.demo.model.Person;


import java.util.stream.Stream;


public interface PersonService {

    Stream<Person> streamAllPersons();
    long countPersons();
}

and here is PersonServiceImpl:

package com.example.demo.database_acess;

import com.example.demo.model.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Stream;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;


    @Transactional
    public Stream<Person> streamAllPersons() {
        return personRepository.findAllByCustomQueryAndStream();
    }

    public long countPersons() {
        return personRepository.count();
    }
}

The ui package consists of the following four files: GUI, PersonsView, PersonsGrid and PersonDataProvider

GUI:

package com.example.demo.ui;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.navigator.Navigator;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.spring.navigator.SpringViewProvider;
import com.vaadin.ui.Panel;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import org.springframework.beans.factory.annotation.Autowired;

@SpringUI
@Title("Demo App")
@Theme("valo")
public class GUI extends UI {

    @Autowired
    private SpringViewProvider viewProvider;

    @Override
    protected void init(VaadinRequest request) {

        VerticalLayout rootLayout = new VerticalLayout();
        rootLayout.setSizeFull();
        setContent(rootLayout);

        Panel viewContainer = new Panel();
        viewContainer.setSizeFull();
        rootLayout.addComponent(viewContainer);
        rootLayout.setExpandRatio(viewContainer, 1.0f);


        Navigator navigator = new Navigator(this, viewContainer);
        navigator.addProvider(viewProvider);

    }

}

PersonsView:

package com.example.demo.ui;

import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.spring.annotation.SpringView;
import com.vaadin.spring.annotation.UIScope;
import com.vaadin.ui.VerticalLayout;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

@UIScope
@SpringView(name= PersonsView.NAME)
public class PersonsView extends VerticalLayout implements View {

    public static final String NAME = "";

    @Autowired
    private PersonsGrid personsGrid;

    @PostConstruct
    void init() {
        setMargin(false);
        addComponent(personsGrid);
        personsGrid.setSizeFull();
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {

    }
}

PersonsGrid:

package com.example.demo.ui;

import com.example.demo.model.Person;
import com.vaadin.spring.annotation.SpringComponent;
import com.vaadin.ui.Grid;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

@SpringComponent
public class PersonsGrid extends Grid<Person> {

    @Autowired
    private PersonDataProvider personDataProvider;

    @PostConstruct
    void init() {
        setDataProvider(personDataProvider);
    }
}

PersonDataProvider:

package com.example.demo.ui;

import com.example.demo.database_acess.PersonServiceImpl;
import com.example.demo.model.Person;
import com.vaadin.data.provider.AbstractBackEndDataProvider;
import com.vaadin.data.provider.Query;
import com.vaadin.spring.annotation.SpringComponent;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.stream.Stream;

@SpringComponent
public class PersonDataProvider extends AbstractBackEndDataProvider<Person, Void> {

    @Autowired
    PersonServiceImpl personService;


    public Stream<Person> fetchFromBackEnd(Query<Person, Void> query) {
        return personService.streamAllPersons();
    }

    public int sizeInBackEnd(Query<Person, Void> query) {
        return (int) personService.countPersons();
    }

}

The application.properties file looks as follows:

spring.datasource.url=jdbc:mysql://localhost:3306/demoDB
spring.datasource.username=root
spring.datasource.password=mypassword
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create

I inserted two persons into the database (Bill and George):

enter image description here

However, these persons are not displayed in the grid, as you can see in the following screenshot:

enter image description here

When writing the code for the Grid (PersonGrid) and the DataProvider (PersonDataProvider), I followed examples from the internet (e.g. http://vaadinhelp.co.in/vaadin-dataprovider-example/)

... My suspicion is that in one of these two files (i.e. PersonGrid and PersonDataProvider) something is missing or wrong.

However, I don't know what.

************************************UPDATE****************************************

I added the following two lines to the init-function of the PersonsView-Class:

personsGrid.addColumn(Person::getName).setCaption("Name");
personsGrid.addColumn(Person::getId).setCaption("ID");

After restarting the application, there are now 3 rows in in the grid (one for the header, one for "George" and one for "Bill").

But: the rows are not correctly displayed, as you can see in the following screenshot:

enter image description here

In the upper left corner you can see the beginning of the three rows but there aren't any columns displayed.

What do I have to do for the data to be displayed correctly?

*******************************UPDATE2******************************************

Apparently, some SQL-operation is not allowed after the ResultSet is closed (see screenshot below). I don't know what this means though.

enter image description here

0

There are 0 answers