Fala ai pessoal! Estou com um problema para popular um DataTable, estou usando JSf, CDI.
Eu acredito que o nome do professor está vindo normal pois ele é um objeto e o aluno é um List, mas nãos ei como resolver isso. Eu estou tentando buscar o atributo nome na classe Usuario que pertence ao Aluno da seguinte forma:
GestaoProjetosBean(meu bean de projetos) -> Aluno.java -> Usuario.java -> nome
Essa é minha página que estou com problemas para trazer o nome do aluno do banco, pois o nome do professor está vindo normal.
GestaoProjetos.xhtml
<!DOCTYPE html> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:p="http://primefaces.org/ui" xmlns:ui="http://xmlns.jcp.org/jsf/facelets" template="/WEB-INF/template/Layout.xhtml"> <ui:define name="titulo"> <title>Gestão Projetos</title> </ui:define> <ui:define name="conteudo"> <f:metadata> <f:viewAction action="#{gestaoProjetosBean.todosProjetos}" /> </f:metadata> <p:dataTable id="projetosDataTable" value="#{gestaoProjetosBean.listaProjetos}" var="projeto" emptyMessage="Nenhuma informação a ser exibida" paginator="true" rows="10" paginatorPosition="bottom" selectionMode="single" selection="#{gestaoProjetosBean.projeto}" rowKey="#{projeto.id}"> <p:ajax event="rowSelect" update="frm:toolbar" /> <p:ajax event="rowUnselect" update="frm:toolbar" /> <p:column headerText="Título" sortBy="#{projeto.titulo}"> <h:outputText value="#{projeto.titulo}" /> </p:column> <!--AQUI O PROFESSOR VEM NORMAL --> <p:column headerText="Professor responsável"> <h:outputText value="#{projeto.professor.usuario.nome}" /> </p:column> <!-- AQUI É O PROBLEMA QUE NÃO VEM O NOME ALUNO --> <p:column headerText="Aluno" > <h:outputText value="#{projeto.aluno.usuario.nome}" /> </p:column> </p:dataTable> </ui:composition>
GestaoProjetosBean.java(Meu bean)
@Named @ViewScoped public class GestaoProjetosBean implements Serializable { private static final long serialVersionUID = 1L; @Inject private Projetos projetos; @Inject private FacesMessages messages; @Inject private CadastroProjetoService cadastroProjetoService; private List<Projeto> listaProjetos; private String termoPesquisa; private Projeto projeto; public void prepararNovoProjeto() { projeto = new Projeto(); this.projeto.setUsuario(new Usuario()); //this.projeto.setAluno(new Aluno()); this.projeto.setProfessor(new Professor()); } public void prepararEdicao() { } public void salvar() { cadastroProjetoService.salvar(projeto); atualizarRegistros(); messages.info("Projeto salvo com sucesso!"); RequestContext.getCurrentInstance().update(Arrays.asList( "frm:monografiasDataTable", "frm:messages")); } public void excluir() { cadastroProjetoService.excluir(projeto); projeto = null; atualizarRegistros(); messages.info("Projeto excluída com sucesso!"); } public void pesquisar() { listaProjetos = projetos.pesquisar(termoPesquisa); if (listaProjetos.isEmpty()) { messages.info("Sua consulta não retornou registros."); } } public void todosProjetos() { listaProjetos = projetos.todas(); } private void atualizarRegistros() { if (jaHouvePesquisa()) { pesquisar(); } else { todosProjetos(); } } private boolean jaHouvePesquisa() { return termoPesquisa != null && !"".equals(termoPesquisa); } public List<Projeto> getListaProjetos() { return listaProjetos; } public String getTermoPesquisa() { return termoPesquisa; } public void setTermoPesquisa(String termoPesquisa) { this.termoPesquisa = termoPesquisa; } public Projeto getProjeto() { return projeto; } public void setProjeto(Projeto projeto) { this.projeto = projeto; } public boolean isProjetoSeleciona() { return projeto != null && projeto.getId() != null; } }
Aluno.java
@Entity @Table(name="aluno") public class Aluno implements Serializable{ private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotNull @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "usuario_id", nullable = false) private Usuario usuario; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Usuario getUsuario() { return usuario; } public void setUsuario(Usuario usuario) { this.usuario = usuario; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Aluno other = (Aluno) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } @Override public String toString() { return "Aluno [id=" + id + "]"; } }
Usuario.java
@Entity @Table(name="usuario") public class Usuario implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotEmpty @Column(nullable = false, length = 40) private String nome; @NotEmpty @Email @Column(nullable = false, length = 40) private String email; @NotNull @Column(nullable = false, length = 8) private Long matricula; @NotEmpty @Column(nullable = false, length = 8) private String senha; @Column(name = "ultimo_acessso") @Temporal(TemporalType.DATE) private Date ultimoAcesso; @Transient private Professor professor; @Transient private List <Aluno> aluno; public Usuario() { super(); } public Usuario(Long matricula, String senha) { super(); this.matricula = matricula; this.senha = senha; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getSenha() { return senha; } public void setSenha(String senha) { this.senha = senha; } public Date getUltimoAcesso() { return ultimoAcesso; } public void setUltimoAcesso(Date ultimoAcesso) { this.ultimoAcesso = ultimoAcesso; } public Long getMatricula() { return matricula; } public void setMatricula(Long matricula) { this.matricula = matricula; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Professor getProfessor() { return professor; } public void setProfessor(Professor professor) { this.professor = professor; } public List<Aluno> getAluno() { return aluno; } public void setAluno(List<Aluno> aluno) { this.aluno = aluno; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Usuario other = (Usuario) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } @Override public String toString() { return "Usuario [id=" + id + "]"; } }
Projeto.java
@Entity @Table(name = "projeto") public class Projeto implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotEmpty @Column(nullable = false, length = 120) private String titulo; @NotEmpty @Column(nullable = false) private String descricao; @NotNull @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "professor_id", nullable = false) private Professor professor; @ManyToMany @JoinTable(name="projeto_aluno", joinColumns= {@JoinColumn(name="projeto_id")}, inverseJoinColumns= {@JoinColumn(name="aluno_id")}) private List <Aluno> aluno; @Transient private Usuario usuario; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitulo() { return titulo; } public void setTitulo(String titulo) { this.titulo = titulo; } public String getDescricao() { return descricao; } public void setDescricao(String descricao) { this.descricao = descricao; } public Professor getProfessor() { return professor; } public void setProfessor(Professor professor) { this.professor = professor; } public List<Aluno> getAluno() { return aluno; } public void setAluno(List<Aluno> aluno) { this.aluno = aluno; } public Usuario getUsuario() { return usuario; } public void setUsuario(Usuario usuario) { this.usuario = usuario; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Projeto other = (Projeto) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } @Override public String toString() { return "Projeto [id=" + id + "]"; } }