I am using EJB with wildfly 11.
For my example i created 2 prjects:
AppBackend
AppFrontend
!!! In the Projekt AppBackend i have the following 3 classes !!!
package org.app.entity;
@NamedQuery(name = Title.QUERY_GETALL, query = "SELECT c FROM Title c")
public class Title implements Serializable {
public static final String QUERY_GETALL = "Title.GetAll";
@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;
}
}
package org.app.ejb.dao;
import org.app.entity.Title;
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();
}
package org.app.ejb.beans;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.app.ejb.dao.TitleDAO;
import org.app.entity.Title;
@Stateless
@Remote(TitleDAO.class)
public class TitleEJB implements TitleDAO {
@PersistenceContext
private EntityManager em;
@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();
}
}
!!! In the Projekt AppFrontend i have the following 3 classes !!!
package org.app.service;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import org.app.ejb.dao.TitleDAO;
import org.app.entity.Title;
@RequestScoped
@Named("title")
public class TitleService {
@EJB
private TitleDAO titleDAO;
private Title title;
public List<Title> getAllTitle() {
List<Title> list = titleDAO.getAllTitle();
return list;
}
public Title getTitle(int id) {
return title = titleDAO.getTitle(id);
}
}
package org.app.ui;
@Theme("appui")
@CDIUI("")
@PushStateNavigation
public class MainView extends UI {
@Inject
TitleService service;
private Navigator navigator;
@Override
protected void init(VaadinRequest request) {
final VerticalLayout mainLayout = new VerticalLayout();
final CssLayout menuView = new CssLayout();
final CssLayout contentView = new CssLayout();
mainLayout.addComponent(menuView);
mainLayout.addComponent(contentView);
mainLayout.setMargin(true);
mainLayout.setSpacing(true);
setContent(mainLayout);
TitleView titleView = new TitleView(service);
navigator = new Navigator(this, contentView);
navigator.addView("", new TitleView(service));
navigator.addView("title", titleView);
navigator.setErrorView(titleView);
}
}
package org.app.ui.title;
import org.app.entity.Title;
import org.app.service.TitleService;
public class TitleView extends VerticalLayout implements View {
public TitleView(TitleService service) {
setSizeFull();
setSpacing(true);
Grid<Title> grid = new Grid<>(Title.class);
List<Title> list = service.getAllTitle();
grid.setItems(list);
addComponent(grid);
}
}
The example above works.
TitleView must be a seperate Class/View/…, it should not be the MainView.
My question is: How is the best practice to show in Vaadin the Content of the EJB Title in a Grid?
I think there must be a more elegant way.