Loading data from Spring JPA --> Visible in console when printing but not v

Hello everybody,

I am trying to retrieve data from a MySQL DB. I retrieve the data and in the debugger I can see the everything is there. I can also print it in the console.
However, because of the @PostContruct, nothing is displayed on the web page. I have no error.

What do I do wrong?

I appreciate the help :slight_smile:

@SpringComponent
public class TestEmployeeLayout extends VerticalLayout {

    @Autowired
    EmployeeRepository repository;
	
    @PostConstruct
    public void init(){
        H1 header = new H1("Hello World");
        System.out.println("________________________________Before adding Header");
        add(header);
        System.out.println("________________________________After adding Header");
        setEmployees(repository.findAll());
    }
	.
	.
	.
	
	private void setEmployees(List<Employee> employees) {

        employees.forEach(employee -> add(new TestOneEmployeeLayout(employee)));

    }
	
public class TestOneEmployeeLayout extends HorizontalLayout {


    public TestOneEmployeeLayout(Employee employee) {

        System.out.println("PRINT First Name-------------------------------------------------------");
        System.out.println(employee.getFirstName());

        TextField firstName = new TextField(employee.getFirstName());
        TextField lastName = new TextField(employee.getLastName());


        add(firstName, lastName);
        System.out.println("After adding name-------------------------------------------------------");

    }

My guess: you are creating the TestEmployeeLayout with the new keyword (like someLayout.add(new TestEmployeeLayout()); so it’s not actually a Spring managed bean → @PostConstruct is never called. Instead, you should make it a @Route or autowire it.

Thanks for the answer :slight_smile: however, the @PostContruct is called : I can print in the console the data. That works. It is the Vaadin components that aren’t displayed on the screen.
So the “add()” part which doesn’t work.

The top view with all the layout is the one with the Route:

@Route(value="EmployeeView", layout=MainView.class)
public class EmployeeView extends VerticalLayout  {

    @PostConstruct
    public void init(){
        setDefaultHorizontalComponentAlignment(Alignment.CENTER);

        TestEmployeeLayout test = new TestEmployeeLayout();
        add(test);
    }
    

}

TestEmployeeLayout test = new TestEmployeeLayout();

Hi Jaufray

Whenever you instantiate a class using the new keyword, then any autowiring will not work! Please read [this answer]
(https://stackoverflow.com/a/59410169/3441504) which explains this further. The postconstruct may be invoked but the autowiring still wont work.

Here is how you can make it work:

@Route(value="EmployeeView", layout=MainView.class)
public class EmployeeView extends VerticalLayout  {

	@Autowired
	private TestEmployeeLayout test;

    @PostConstruct
    public void init(){
        setDefaultHorizontalComponentAlignment(Alignment.CENTER);
        //TestEmployeeLayout test = new TestEmployeeLayout(); //instance is already autowired
        add(test);
    }
}

You can change the field injection of the TestEmployeeLayout to constructor injection, making the autowired layout present during constructor. This will make the @PostConstruct obsolete.

@Route(value="EmployeeView", layout=MainView.class)
public class EmployeeView extends VerticalLayout  {
	@Autowired
    public EmployeeView(TestEmployeeLayout test){
        setDefaultHorizontalComponentAlignment(Alignment.CENTER);
        add(test);
    }
}

However, if you want to instantiate the TestEmployeeLayout yourself using the new keyword, you can autowire the EmployeeRepository in the EmployeeView and pass the repository instance into the constructor of the TestEmployeeLayout:

@Route(value="EmployeeView", layout=MainView.class)
public class EmployeeView extends VerticalLayout  {
	@Autowired
    public EmployeeView(EmployeeRepository employeeRepository){
        setDefaultHorizontalComponentAlignment(Alignment.CENTER);
		TestEmployeeLayout test = new TestEmployeeLayout(employeeRepository);
        add(test);
    }
}

public class TestEmployeeLayout extends VerticalLayout {
    public TestEmployeeLayout(EmployeeRepository employeeRepository){
        H1 header = new H1("Hello World");
        System.out.println("________________________________Before adding Header");
        add(header);
        System.out.println("________________________________After adding Header");
        setEmployees(employeeRepository.findAll());
    }
	
	private void setEmployees(List<Employee> employees) {
        employees.forEach(employee -> add(new TestOneEmployeeLayout(employee)));
    }
}

Hi Kaspar,

Ahhh okay I understand. Thank you so much! It works well now :slight_smile: