Issues in netbeans with connection to Postgres

Asked

Viewed 343 times

2

I have a bank called BANCO_DE_TESTES in Postgres that has some random tables. In netbeans when I try to use the Master Sample Form/Detail to use JPA to create a simple crud it doesn’t work saying that the database I specified has no entities (tables).
But when I go to the netbeans services tab and make a connection with this database it normally opens all tables of the database in question (BANCO_DE_TESTES) and allows you to make any kind of query about them!!!!

What should I do?
With mysql there is no problem!!! But I want to use Postgres!!!

inserir a descrição da imagem aqui

Here I open normally as you can check:

inserir a descrição da imagem aqui

1 answer

2


As for Netbeans I can’t say, what’s the problem.

But one of the reasons may be that you are accessing the wrong schema.

But to create tables with JPA, you only need to create the data and JPA with Hibernate for example, is in charge of creating the tables according to the annotated classes, if using DDL.

Generating code is never recommended, if it doesn’t work, try creating it manually.

Mapping the classes you want to create and having your CRUD on a DAO.

  @Entity
  public class Usuario {

     @Id
     @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "usuarios_seq")
     @SequenceGenerator(name = "usuarios_seq", sequenceName = "usuarios_id_seq")
     private Long id;

     private String nome;
     private String email;
     private String senha;

     //getters e setters
  }

Now the DAO class with a CRUD

public class UsuarioDao {

        private EntityManager entityManager;

        public UsuarioDao() {
            entityManager = getEntityManager(); //Ex: Recebe um EntityManager pelo construtor
        }

        public Usuario usuarioPorId(int id) {
            return entityManager.find(Usuario.class, id);
        }

        public List<Usuario> todosUsuarios() {
            return entityManager.createQuery("SELECT u FROM Usuario u", Usuario.class).getResultList();
        }

        public void gravar(Usuario usuario) {
            try {
                entityManager.getTransaction().begin();
                entityManager.persist(usuario);
                entityManager.getTransaction().commit();
            } catch (Exception ex) {
                ex.printStackTrace();
                entityManager.getTransaction().rollback();
            }
        }

        public void atualizar(Usuario usuario) {
            try {
                entityManager.getTransaction().begin();
                entityManager.merge(usuario);
                entityManager.getTransaction().commit();
            } catch (Exception ex) {
                ex.printStackTrace();
                entityManager.getTransaction().rollback();
            }
        }

        public void remover(Usuario usuario) {
            try {
                entityManager.getTransaction().begin();
                usuario = entityManager.find(Usuario.class, usuario.getId());
                entityManager.remove(usuario);
                entityManager.getTransaction().commit();
            } catch (Exception ex) {
                ex.printStackTrace();
                entityManager.getTransaction().rollback();
            }
        }
    }

Browser other questions tagged

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