Problems in netbeans with connection to Postgres

2

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

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

HereIusuallyopenasyoucancheckout:

    
asked by anonymous 19.12.2015 / 19:49

1 answer

2

As for Netbeans, I do not know what the problem is.

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

  

But to create tables with JPA, you just need to create the database   data and JPA with Hibernate for example, is responsible for creating the   tables according to the annotated classes, if you are using   DDL.

Generating code is never recommended, if it does not work, try to create it manually.

Mapping the classes you want to create and taking your CRUD into 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();
            }
        }
    }
    
19.12.2015 / 21:01