0

I bound several text fields to an entity in the IDE Rapidclipse that uses Vaadin 14 and JPA/Hibernate. The aim is to write the input data on button click into the referring database that is defined as datasource. I already found this documentation Writing manually. So my understanding of this is like that: At first i need to create a new bean of the entity (Test test = new Test()). Then all input data should be assigned to the attributes of the bean(binder.writeBean(test)). That's as far as the documentation goes. But how does the data get inserted into the table of the entity in my database? Do i need the TestDAO class?

In Vaadin 7, which was used in the former Rapidclipse version, it worked like this:

fieldGroup.setItemDatasource(new Test());
...
fieldgroup.save()

1 Answers1

2

Binder is used to bind attributes of a Bean to a form(TextFields, for example) displayed in UI. So when you are doing something like this How to Bind Form Data

Binder<Person> binder = new Binder<>(Person.class);

TextField titleField = new TextField();

// Start by defining the Field instance to use
binder.forField(titleField)
        // Finalize by doing the actual binding
        // to the Person class
        .bind(
                // Callback that loads the title
                // from a person instance
                Person::getTitle,
                // Callback that saves the title
                // in a person instance
                Person::setTitle);

you are exactly stating that titleField displays a title of a person. Binder is not related to/responsible of a DB persistence. In the documentation you linked, in all examples there is a row similar to this: MyBackend.updatePersonInDatabase(person);. It's a responsibility of a developer to persist object into underlying database. Flow is a UI framework and that is the reason you are free to choose a database provider/technology.

Regarding Vaadin 7, where you have found an example? I can't find a method save on a FieldGroup class FieldGroup API

Maybe someone has extended it and added a needed functionality?

So, for example, if you are using hibernate something along the line of this:

session.save(emp); will insert your object into DB. Taken from here: Hibernate Insert Query Tutorial

Some other useful links:

anasmi
  • 2,562
  • 1
  • 13
  • 25
  • 1
    So nothing has change in this sense between Vaadin 7 and Vaadin 14. You can use the same logic as before. `save(entity)` method still takes a object it needs to persist. In your case it's the one written to binder – anasmi Oct 11 '19 at 06:59
  • And regarding `DAO`. It's up to you, how you would like to implement persistense. `DAO` adds level of abstraction. [Data access object (DAO) in Java](https://stackoverflow.com/questions/19154202/data-access-object-dao-in-java) – anasmi Oct 11 '19 at 07:00
  • Thank you, it worked. It's pretty obvious. I don't know why i did not get it. –  Oct 11 '19 at 07:09
  • No problem! Great that it works now and we all have been there trying to narrow down some problem :) – anasmi Oct 11 '19 at 07:10