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 you run, everything is created but the information is not entered in the database.