EJB in vaadin

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.

Is your question, that can you reduce number of the classes in the implementation. The short answer is not really. That is not actually not that much about Vaadin, but in general how Java application stacks work. In CRUD applications you can use database entities directly in UI, but there are some caveats. For example if you update item the database, you need to refetch them so that entity versions in memory and database match. Otherwise you will easily get optimistic locking exceptions, or entity not found exceptions, etc. In more complex applications we add DTO layer in business logic. Again that is not Vaadin thing, but generic pattern how things work in Java.

Can you do something to ease your work, yes. There are tools like Deltaspike that reduce burden of writing boiler plate code, see example here. In your simple example it really does not matter much, but when business logic grows and repository becomes more complex, it helps

https://github.com/mstahv/jpa-addressbook/tree/master/src/main/java/org/example

I need the architecture of the Application, because the application is very complex.
I need the project AppBackend with the 3 classes.

My Question is:
How can i show the Data in a Grid in Vaadin when i use Java EE 7 with EJB?
I have to implement a lot of Grids from ohter EJB’s

In that case I would recommend to study this reference application we have made

https://vaadin.com/start/v8-full-stack-javaee

It demonstrates what kind of architecture we prefer in medium sized application. There is also example of Grid with list of Orders, which has implementation for lazy loading data provider with entities fetched via Deltaspike repo.