Hello, i am using vaadin 8 with wildfly 11.
In the Backend I have the following 3 Classes
- Entity
- EJB
- DAO
Entity:
@Entity
@NamedQuery(name = Title.QUERY_GETALL, query = "SELECT c FROM Title c")
public class Title implements Serializable {
private static final long serialVersionUID = 1L;
public static final String QUERY_GETALL = "Title.GetAll";
private Timestamp createAT;
private Timestamp modifyAT;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@NotNull
private String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@PrePersist
protected void onCreate() {
createAT = new Timestamp(System.currentTimeMillis());
}
@PreUpdate
protected void onUpdate() {
modifyAT = new Timestamp(System.currentTimeMillis());
}
}
EJB:
@Stateless
@Remote(TitleDAO.class)
public class TitleEJB implements TitleDAO {
@PersistenceContext
private EntityManager em;
@Override
public Title create(Title title) {
em.persist(title);
return title;
}
@Override
public Title update(Title title) {
return em.merge(title);
}
@Override
public void remove(int id) {
Title toBeDeleted = getTitle(id);
em.remove(toBeDeleted);
}
@Override
public Title getTitle(int id) {
return em.find(Title.class, id);
}
@Override
public List<Title> getAllTitle() {
return em.createNamedQuery(Title.QUERY_GETALL, Title.class).getResultList();
}
}
DAO:
public interface TitleDAO {
public Title create(Title title);
public Title update(Title title);
public void remove(int id);
public Title getTitle(int id);
public List<Title> getAllTitle();
}
In the Frontend I want now to bind the Entity of the Backend to a Vaadin Grid and make it editable:
@SuppressWarnings("serial")
@CDIView("TitleView")
public class TitleView02 extends VerticalLayout implements View {
private Button add;
private Set<Title> selected;
private Title title;
public TitleView02(TitleService service) {
final List<Title> allTitles = service.getAllTitle();
// Empty Grid, must add Columns to show by hand
Grid<Title> grid = new Grid();
grid.setSelectionMode(SelectionMode.MULTI);
grid.getEditor().setEnabled(true);
grid.setDataProvider(DataProvider.ofCollection(allTitles));
grid.addColumn(Title::getListPrio).setCaption("ListPriority");
grid.addColumn(title -> title.getTitle()).setCaption("-Titel-").setEditorComponent(new TextField(), Title::setTitle);
add = new Button("+");
add.addClickListener(event -> addRow(service, grid));
HorizontalLayout tb = new HorizontalLayout(add);
addComponent(grid);
addComponent(tb);
}
private void addRow(TitleService service, Grid<Title> grid) {
int maxListPrio;
Title title = new Title();
/*
* Get max of ListPrio
*/
List<Title> list = service.getAllTitle();
maxListPrio = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getListPrio() > maxListPrio) {
maxListPrio = list.get(i).getListPrio();
}
}
title.setListPrio(maxListPrio + 1);
title.setTitle("");
list.add(title);
grid.setItems(list);
service.create(title);
refreshGrid(grid, service);
}
private void refreshGrid(Grid<Title> grid, TitleService service) {
List<Title> list = service.getAllTitle();
grid.setItems(list);
}
}
The Grid is editable.
But the data are not saved in the Database.
What must i do, that the data are saved in the database?