DigaoParceiro DigaoParceiro - 1 year ago 117
Java Question

Hibernate JAVA | How to get an instance of EntityManager WITHOUT creating a new table?

every time that I want to get the instance of Entity Manager is creating new tables in the database, so how I can get the EntityManager (to insert, delete, update the database) in Postgresql without creating new tables?

Entity "Medico":

@Table(name = "medico")
@SequenceGenerator (name = "medico_seq",sequenceName="medico_seq",allocationSize = 1, initialValue = 1)
public class Medico implements Serializable{
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = "medico_seq")
private Integer id = null;
private String especializacao;
private Integer diasRetorno;
private Time tempoConsulta;
private String nome;
private String cpf;
private String email;
private String senha;
private String telefone;
private String cidade;
private String estado;
private String cep;
private String endereco;
private String bairro;
private Integer enderecoNumero;

public Medico(String especializacao, Integer diasRetorno, Time tempoConsulta, String nome, String cpf, String email, String senha, String telefone, String cidade, String estado, String cep, String endereco, String bairro, Integer enderecoNumero) {
//getters and setters


package dao;

import entidade.Medico;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.hibernate.Session;

public class MedicoDAO {

public Medico salvar (Medico medico){

EntityManager em = Persistence.createEntityManagerFactory("PersistenciaPU").createEntityManager();
em.persist(medico); //Faz o insert
medico = em.merge(medico); // Faz o update

}catch(Exception ex){
System.err.println("ERRO123: "+ex.getMessage());
return medico;



<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="PersistenciaPU" transaction-type="RESOURCE_LOCAL">
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/clinica"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.password" value="123456"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>


package persistencia;

import dao.Auxiliar;
import dao.MedicoDAO;
import entidade.Medico;
import java.sql.Time;
import javax.persistence.Persistence;

public class Persistencia {

public static void main(String[] args) {
Time t = new Time(2,3,4);
Medico medico = new Medico("Cirurgiao",7,t,"Roberto","555.458.912-12","roberto@medico.com","12345","48 3275 0463", "Florianópolis", "SC", "88.222-200","Rua Bonita","Lagoa",12);

MedicoDAO dao = new MedicoDAO();


Exception in thread "main" javax.persistence.PersistenceException: Unable to execute JPA schema generation create command [create table medico]

Caused by: org.postgresql.util.PSQLException: ERROR: relation "medico" already exists

Answer Source

I can't see the Image. but try to change

<property name="hibernate.hbm2ddl.auto">create</property> // create new schema

to this:

<property name="hibernate.hbm2ddl.auto">update</property> // update the schema
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download