JPA - Java does not insert data into the database

Asked

Viewed 104 times

0

Bean

 @Named("proprietarioMB")
     public class ProprietarioBean  implements Serializable {
    private static final long serialVersionUID = 1L;

    @Inject
    private ProprietarioService proprietarioService;

    @Inject
    private Proprietario proprietario = null;

    private String dataTemp;

    public Proprietario getProprietario() {
        return proprietario;
    }

    public void setProprietario(Proprietario proprietario) {
        this.proprietario = proprietario;
    }



    public String getDataTemp() {
        return dataTemp;
    }

    public void setDataTemp(String dataTemp) {
        this.dataTemp = dataTemp;
    }



    private void stringToDate(){
        SimpleDateFormat tmp = new SimpleDateFormat("dd/MM/yyyy");
        try {
            Date date = tmp.parse(getDataTemp());
            proprietario.setDataNascimento(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void InsereProprietario(){
        //stringToDate();


                proprietarioService.criar(proprietario);


    }


}

Model

    @Entity
public class Proprietario implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Column(name ="cpfProprietario")
    private int cpf;

    @Column(name ="nome" ,length=100)
    private String nome;

    @Column(name ="endereco" ,length=100)
    private String endereco;

    @Column(name ="bairro" , length=100)
    private String bairro;

    @Column(name ="cidade" ,length=100)
    private String cidade;

    @Column(name ="uf" ,length=2)
    private String uf;


    @Column(name ="sexo" ,length=1)
    private String sexo;

    @Column(name ="dtNascimento")
    private Date dataNascimento;

    public int getCpf() {
        return cpf;
    }

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

    public String getNome() {
        return nome;
    }

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

    public String getEndereco() {
        return endereco;
    }

    public void setEndereco(String endereco) {
        this.endereco = endereco;
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        this.bairro = bairro;
    }

    public String getCidade() {
        return cidade;
    }

    public void setCidade(String cidade) {
        this.cidade = cidade;
    }

    public String getUf() {
        return uf;
    }

    public void setUf(String uf) {
        this.uf = uf;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

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


}

Service

public class ProprietarioService implements Serializable {
     @Inject
     private ProprietarioRepository proprietarioRepository;

     //CRIAR

     public void criar(Proprietario proprietario){

         proprietarioRepository.criar(proprietario);


     }

     //EDITAR

     //DELETAR

     //BUSCAR

}

Repository

    public class ProprietarioRepository implements Serializable {

    @Inject 
    private EntityManager manager;


    public void criar(Proprietario proprietario) {
        try{

            this.manager.persist(proprietario);

        }catch(Exception e){
            System.out.println("ERROR");
        }


    }

}

on my console print nothing, some north?

2 answers

0

How is your setup with the database?

Apparently the transaction is missing there.

Before the persist do:

manager.getTransaction().begin();

And after the persist:

manager.getTransaction().commit();

Ideally, the transaction should be in a much larger scope than just saving it, for example during the entire request

  • when I use manager.getTransaction(). Begin(); it says it’s not to use "The JTA Entitymanager cannot use getTransaction()"

0

Try replacing the @Injectof your EntityManager for @PersistenceContext. Depending on how you set up your persist.xml (or another name for the JPA implementation configuration file) it is necessary to open and close the transaction as mentioned above.

Browser other questions tagged

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