Bom dia,
Ao implementar um CRUD, surgiu uma pequena duvida:
alunos.xhtml:
[code]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f=“http://java.sun.com/jsf/core”>
<h:head>
<title>Alunos</title>
</h:head>
<h:body>
<h1>Novo Aluno</h1>
<h:form>
<h:outputLabel value=“Nome: " />
<h:inputText value=”#{alunoMB.aluno.nome}" />
<h:outputLabel value="Matrícula: " />
<h:inputText value="#{alunoMB.aluno.matricula}" />
<h:commandButton action="#{alunoMB.adiciona}" value="Salvar" />
</h:form>
<h:form>
<h1>Lista de Livros</h1>
<h:dataTable value="#{alunoMB.alunos}" var="aluno">
<h:column>
<h:outputText value="#{aluno.nome}" />
</h:column>
<h:column>
<h:outputText value="#{aluno.matricula}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>[/code]
AlunoMB.class:
package managedbeans;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.inject.Named;
import entidades.Aluno;
import sessionbeans.AlunoRepository;
@Named
@SessionScoped
public class AlunoMB implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private AlunoRepository alunoRepository;
private Aluno aluno = new Aluno();
private List<Aluno> alunosCache;
public Aluno getAluno() {
return aluno;
}
public void setAluno(Aluno aluno) {
this.aluno = aluno;
}
public void adiciona() {
this.alunoRepository.adiciona(this.aluno);
this.aluno = new Aluno();
this.alunosCache = null;
}
public void atualiza(Aluno aluno) {
this.alunoRepository.atualiza(aluno);
this.aluno = new Aluno();
this.alunosCache = null;
}
public void procura(Aluno aluno) {
this.alunoRepository.procura(aluno.getId());
this.aluno = new Aluno();
this.alunosCache = null;
}
public void remove(Aluno aluno) {
this.alunoRepository.remove(aluno.getId());
this.aluno = new Aluno();
this.alunosCache = null;
}
public List<Aluno> getAlunos() {
if (this.alunosCache == null) {
this.alunosCache = this.alunoRepository.getAlunos();
}
return this.alunosCache;
}
}
AlunoRepository.class:
package sessionbeans;
import java.io.Serializable;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import entidades.Aluno;
@Stateless
public class AlunoRepository implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@PersistenceContext
private EntityManager entityManager;
public void adiciona(Aluno aluno) {
this.entityManager.persist(aluno);
}
public void atualiza(Aluno aluno) {
this.entityManager.merge(aluno);
}
public Aluno procura(Long id) {
return this.entityManager.find(Aluno.class, id);
}
public void remove(Long id) {
Aluno aluno = this.entityManager.find(Aluno.class, id);
this.entityManager.remove(aluno);
}
public List<Aluno> getAlunos() {
TypedQuery<Aluno> query = this.entityManager.createQuery(
"select x from Aluno x", Aluno.class);
return query.getResultList();
}
}
Em um determinado capítulo da apostila da Caelum, é proposto um pequeno exercício, a minha solução é exibida logo acima. Bom, eu gostaria de saber se a Expression Language deveria chamar diretamente a camada de negócio ou se o EJB suporta, em termos de padrão de arquitetura, a minha solução?
Desde já eu agradeço!
[]'s