Error inserting data into JPA

Asked

Viewed 46 times

0

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
// pode usar para alterar o nome da tabela la no banco
//@Table(name = "")
public class Usuario {

    @Id
    @GeneratedValue
    private Long id;
    @Column(name= "login_usu")
    private String login;
    private String senha;

    public void mostrar() {
        System.out.printf("Id: %-5d Login: %-30s Senha: %-20s \n", id, login, senha);
    }

    public Long getId() {
        return id;
    }

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

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

}
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class ConnectionFactory {

    private static EntityManagerFactory emf = Persistence.createEntityManagerFactory("aula09");
    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

}
import javax.persistence.EntityManager;

public class Principal {

    public static void main(String[] args) {

        Usuario u = new Usuario();
        u.setLogin("[email protected]");
        u.setSenha("senha123");

        EntityManager em = ConnectionFactory.getEntityManager();
        em.getTransaction().begin();
        em.persist(u);
        em.getTransaction();
        em.close();

    }
}

When ordered to run, everything is created but information is not inserted in the database.

  • 1

    Your password field has no annotation.

  • 1

    I edited your question because you are using JPA, not JDBC directly. Also, I took out the [sql] tag because you are not having trouble with SELECTs, INSERTs, UPDATEs and DELETEs - you are not using them directly.

1 answer

2

Assuming your settings on persistence.xml are correct, do the following:

  1. "Write down" your attribute senha with @Column(name="NOME_DA_COLUNA")
  2. In the row following the method persist(u), call the method commit() with: em.getTransaction().commit();

In the end it will be like this (briefly):

@Entity
public class Usuario {

    @Id
    @GeneratedValue
    private Long id;

    @Column(name= "login_usu")
    private String login;

    @Column(name="NOME_DA_COLUNA")
    private String senha;

    //getters e setters

}

public class Principal {

    public static void main(String[] args) {

        Usuario u = new Usuario();
        u.setLogin("[email protected]");
        u.setSenha("senha123");

        EntityManager em = ConnectionFactory.getEntityManager();
        em.getTransaction().begin();
        em.persist(u);
        em.getTransaction().commit();
        em.close();
    }
}
  • 1

    maaaano, you are very wild. I stayed long and did not see. vlw of more++++

  • 1

    @user66600 If this answer has solved your problem and there is no question or doubt left, mark it as accepted/correct by clicking the next one. This marks your question as solved/solved.

  • I can’t find this place to talk about

  • @user66600 has a check sign (in gray) just below the response votes, if I’m not mistaken

  • @user66600 please mark the answer as solution.. If you go unsolved all the time someone will come in here and after reading everything you will see that the problem has been solved

Browser other questions tagged

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