CDI with EJB not possible

I am using javaEE with wildfly and vaadin 8.

I have a Backend with the following 3 Classes;
All is here ok.

  • Entity
  • EJB
  • DAO
@Entity
@Table(name = "ACCOUNT")
@SuppressWarnings("all")
@NamedQuery(name = Account.QUERY_GETALL, query = "SELECT c FROM Account c")
public class Account implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final String QUERY_GETALL = "Account.GetAll";
	private Timestamp createAT;
	private Timestamp modifyAT;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	private String username;

	private String mailaddress;

	@NotNull
	private String password;
	
	private String comment;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getMailaddress() {
		return mailaddress;
	}

	public void setMailaddress(String mailaddress) {
		this.mailaddress = mailaddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	@PrePersist
	protected void onCreate() {
		createAT = new Timestamp(System.currentTimeMillis());
	}

	@PreUpdate
	protected void onUpdate() {
		modifyAT = new Timestamp(System.currentTimeMillis());
	}

	public Timestamp getCreateAt() {
		return createAT;
	}

	public Timestamp getModifyAt() {
		return modifyAT;
	}

}
@Stateless
@Remote(AccountDAO.class)
public class AccountEJB implements AccountDAO {

	@PersistenceContext
	private EntityManager em;

	@Override
	public Account create(Account account) {
		em.persist(account);

		return account;
	}

	@Override
	public Account update(Account account) {
		return em.merge(account);

	}

	@Override
	public void remove(int id) {
		Account toBeDeleted = getAccount(id);
		em.remove(toBeDeleted);
	}

	@Override
	public Account getAccount(int id) {
		return em.find(Account.class, id);
	}

	@Override
	public List<Account> getAllAccount() {
		return em.createNamedQuery(Account.QUERY_GETALL, Account.class).getResultList();
	}

}
public interface AccountDAO {

	public Account create(Account account);

	public Account update(Account account);

	public void remove(int id);

	public Account getAccount(int id);

	public List<Account> getAllAccount();

}

In the Frontend i have the following “Managed Bean”;
All is ok.

@ApplicationScoped
public class AccountService {

	@EJB
	private AccountDAO accountDAO;
	private Account account;

	public List<Account> getAllAccount() {
		List<Account> list = accountDAO.getAllAccount();
		return list;
	}
	
	public Account getAccount(int id) {
		return account = accountDAO.getAccount(id);
	}

	public void remove(int id) {
		accountDAO.remove(id);
	}
	
	public void create(Account account) {
		accountDAO.create(account);
	}

	public void update(Account account) {
		accountDAO.update(account);
	}
}

My Problem is in the View of the Frontend.

I use the Navigator; all is ok.

	Button accountViewButton = new Button(MainView.ACCOUNT_VIEW,
			e -> UI.getCurrent().getNavigator().navigateTo(MainView.ACCOUNT_VIEW));

Here I navigate to the following View and get Problem, when i want to show the data by the “Managed Bean” in the Grid.

@CDIView(MainView.ACCOUNT_VIEW)
public class AccountView extends VerticalLayout implements View {
	
	private AccountService service;
	
	public AccountView() {
		setSizeFull();
		setSpacing(true);
		addComponent(new TopMainMenu());
		addComponent(new Label("Account View"));
		service = new AccountService();
		showData(service);
	}

	@Override
	public void enter(ViewChangeEvent event) {
		Notification.show("Showing view: Account View");
	}
	
	public void showData(AccountService service) {
		List<Account> accounts = service.getAllAccount();
		Grid<Account> grid = new Grid<Account>();
		grid.setItems(accounts);
		addComponent(grid);
	}
}

The following code works:


@CDIView(MainView.ACCOUNT_VIEW)
public class AccountView extends VerticalLayout implements View {
	public AccountView() {
		setSizeFull();
		setSpacing(true);
		addComponent(new TopMainMenu());
		addComponent(new Label("Account View"));
		service = new AccountService();
	}

	@Override
	public void enter(ViewChangeEvent event) {
		Notification.show("Showing view: Account View");
	}
}

But how can i show the Data by the Managed Bean in the Grid in this View?

I think your issue has nothing to do with Java EE, EJB etc.

I guess that in your case you have just empty Grid there, and the reason is that you have not configured the columns of it.

Use Grid.addColumn(…) method to configure them

See documentation here

https://vaadin.com/docs/v8/framework/datamodel/datamodel-providers.html

E.g. in your case something like:

grid.addColumn(Account::getUserName).setCaption("User's name");

I have tried the following code. But it also not works.
I get an error.

@CDIView(MainView.ACCOUNT_VIEW)
public class AccountView extends VerticalLayout implements View {
	
	private AccountService service;
	
	public AccountView() {
		setSizeFull();
		setSpacing(true);
		addComponent(new TopMainMenu());
		addComponent(new Label("Account View"));
		service = new AccountService();
		showData(service);
	}

	@Override
	public void enter(ViewChangeEvent event) {
		Notification.show("Showing view: Account View");
	}
	
	public void showData(AccountService service) {
		List<Account> accounts = service.getAllAccount();
		DataProvider dataProvider = DataProvider.ofCollection(accounts);
		Grid<Account> grid = new Grid<Account>();
		
		grid.setDataProvider(dataProvider);
		grid.addColumn(Account::getUsername).setCaption("User's Name");
		addComponent(grid);
	}
}

Hello
I have solved the problem via @PostConstruct

@CDIView(MainView.ACCOUNT_VIEW)
public class AccountView extends VerticalLayout implements View {
	
	@Inject
	AccountService service;
	
	public AccountView() {
		setSizeFull();
		setSpacing(true);
		addComponent(new TopMainMenu());
		addComponent(new Label("Account View"));
	}

	@Override
	public void enter(ViewChangeEvent event) {
		Notification.show("Showing view: Account View");
	}
	
	@PostConstruct
	void init() {
		List<Account> accounts = service.getAllAccount();
		DataProvider<Account, ?> dataProvider = DataProvider.ofCollection(accounts);
		Grid<Account> grid = new Grid<Account>();
		
		grid.setDataProvider(dataProvider);
		grid.addColumn(Account::getUsername).setCaption("User's Name");
		grid.addColumn(Account::getMailaddress).setCaption("Email Address");
		grid.addColumn(Account::getPassword).setCaption("Password");
		grid.addColumn(Account::getComment).setCaption("Comment");
		addComponent(grid);
	}
}