How to Grab Primary Key and Insert Foreign Key from another JSF Table via Selectonemenu

Asked

Viewed 30 times

0

I’m doing a test project, and I’ve reached a point where I can’t pass a PK of one entity to be a Fk in another, I’m getting this PK ID via selectOneMenu but I try to send the value will not, always stays Null. Have two entities Student and Account, first register the student, then when registering the account this will receive the PK that will turn into FK, but this part I’m not able to get it right. I will show the codes below. I am Using Widfly version 20.

package com.academia.Models;

import java.io.Serializable; import java.util.Date;

import javax.persistence.Entity; import javax.persistence.Generatedvalue; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.Temporaltype;

import org.hibernate.Validator.constraints.br.CPF;

@Entity public class Student Serializable Mplements {

private static final long serialVersionUID = -5843387401969060323L;

@Id
@GeneratedValue
private Integer id;
private String nome;
private String celular;
@CPF
private String cpf;

@Temporal(TemporalType.DATE)
private Date dataNascimento;


public Aluno() {
}

public Integer getId() {
    return id;
}

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

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCpf() {
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

public Date getDataNascimento() {
    return dataNascimento;
}

public void setDataNascimento(Date dataNascimento) {
    this.dataNascimento = dataNascimento;
}

public String getCelular() {
    return celular;
}

public void setCelular(String celular) {
    this.celular = celular;
}

@Override
public String toString() {
    return "Aluno [id=" + id + ", nome=" + nome + ", celular=" + celular + ", cpf=" + cpf + ", dataNascimento="
            + dataNascimento + "]";
}

}

package com.academia.Models;

import java.math.Bigdecimal; import java.util.Date;

import javax.persistence.Cascadetype; import javax.persistence.Entity; import javax.persistence.Generatedvalue; import javax.persistence.Generationtype; import javax.persistence.Id; import javax.persistence.Joincolumn; import javax.persistence.Onetoone;

@Entity public class Account {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cod;
private BigDecimal valor;
private Date dataPagamento;


@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(referencedColumnName = "id")
private Aluno aluno;

public Conta() {
}

public Integer getCod() {
    return cod;
}

public void setCod(Integer cod) {
    this.cod = cod;
}

public Aluno getAluno() {
    return aluno;
}

public void setAluno(Aluno aluno) {
    this.aluno = aluno;
}

public BigDecimal getValor() {
    return valor;
}

public void setValor(BigDecimal valor) {
    this.valor = valor;
}

public Date getDataPagamento() {
    return dataPagamento;
}

public void setDataPagamento(Date dataPagamento) {
    this.dataPagamento = dataPagamento;
}

@Override
public String toString() {
    return "Conta [cod=" + cod + ", valor=" + valor + ", dataPagamento=" + dataPagamento + ", aluno=" + aluno + "]";
}

}

package com.academia.Bean;

import java.io.Serializable; import java.util.Arraylist; import java.util.List;

import javax.enterprise.context.Requestscoped; import javax.inject.Inject; import javax.inject.Named; import javax.transaction.Transactional;

import com.academia.Dao.Alunodao; import com.academia.Models.Student; import com.academia.Util.Mensagens;

@Named @Requestscoped public class Alunobean Implements Serializable {

private static final long serialVersionUID = 1L;

private Aluno aluno = new Aluno();
private List<Aluno> alunosAll = new ArrayList<>();

//variavel que irá receber o valor de ID setado no FORM de escolha. 
private Integer alunoId;

public List<Aluno> getAlunosAll() {
    return alunosAll;
}

public void setAlunosAll(List<Aluno> alunosAll) {
    this.alunosAll = alunosAll;
}

@SuppressWarnings("cdi-ambiguous-dependency")
@Inject
private AlunoDao alunoDao;
@Inject
private Mensagens msg;



public Aluno getAluno() {
    return aluno;
}

public void setAluno(Aluno aluno) {
    this.aluno = aluno;
}

@Transactional // CREATE
public Aluno cadastrarAluno() {

    try {
        alunoDao.insert(aluno);
        msg.info();
        return aluno = new Aluno();

    } catch (Exception e) {
        msg.err();
    }
    return aluno;
}

@Transactional // READY
public List<Aluno> consultarAlunos() {
    this.alunosAll = alunoDao.listar();
    return alunosAll;
}

@Transactional // DELETE
public void remover(Aluno aluno) {

    try {
        alunoDao.remove(aluno);
        msg.deleteSucess();
    } catch (Exception e) {
        msg.err();
    }
}

@Transactional
public void alunoId(Aluno aluno) {
    alunoDao.buscaPorId(aluno);
}

public Integer getAlunoId() {
    return alunoId;
}

public void setAlunoId(Integer alunoId) {
    this.alunoId = alunoId;
}

}

package com.academia.Bean;

import java.util.Arraylist; import java.util.List;

import javax.enterprise.context.Applicationscoped; import javax.inject.Inject; import javax.inject.Named; import javax.transaction.Transactional;

import com.academia.Dao.Contadao; import com.academia.Models.Student; import com.academia.Models.Conta;

@Named @Applicationscoped public class Contabean {

@SuppressWarnings("cdi-ambiguous-dependency")
@Inject
private ContaDao contaDao;

private Conta conta = new Conta();

private List<Aluno> alunos = new ArrayList<>();

public Conta getConta() {
    return conta;
}

public void setConta(Conta conta) {
    this.conta = conta;
}

@Transactional
public List<Aluno> buscarAluno() {
    this.alunos = contaDao.listarPConta();
    return alunos;
}

@Transactional // CREATE
public void cadastrarConta() {
    System.out.println(conta);
}
    

}

<h:form>

    <p:outputLabel for="alunos"/>     <!-- Variavel que irá retornar o valor de aluno.id -->
        <h:selectOneMenu id="alunos" value="#{alunoBean.alunoId}"  converter="javax.faces.Integer" >
            <f:selectItems 
            value="#{alunoBean.consultarAlunos()}" 
            var="aluno"
            itemLabel="#{aluno.nome}"
            itemValue="#{aluno.id}" />              
        </h:selectOneMenu>
    
    
    <p:outputLabel for="nome" />
        <p:inputText id="nome" value="#{contaBean.conta.valor}" />

    <p:commandButton action="#{contaBean.cadastrarConta()}"
        value="Cadastrar" />

</h:form>

As You can see in Contabean the Method to register, for now I’m just viewing in the Console and I realize that the value related to student, only arrives Null

Console snippet after execution (Account [Cod=null, value=10.00, timePath=null, student=null])

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.