每次我想要获取Entity Manager的实例时,都是在数据库中创建新表,那么如何在不创建新表的情况下获取Postgresql中的EntityManager(插入、删除、更新数据库)?
实体“医疗公司”:
@Entity
@Table(name = "medico")
@SequenceGenerator (name = "medico_seq",sequenceName="medico_seq",allocationSize = 1, initialValue = 1)
public class Medico implements Serializable{
@Id
@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();
try{em.getTransaction().begin();
if(medico.getId()==null){
em.persist(medico); //Faz o insert
}
else{
medico = em.merge(medico); // Faz o update
}
em.getTransaction().commit();
}catch(Exception ex){
System.err.println("ERRO123: "+ex.getMessage());
}finally{
em.close();
}
em.close();
return medico;
}
}
“持久性.xml”
<?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">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>entidade.HorarioAtendimentoMedico</class>
<class>entidade.Medico</class>
<class>entidade.Paciente</class>
<properties>
<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"/>
</properties>
</persistence-unit>
</persistence>
主要:
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","[email protected]","12345","48 3275 0463", "Florianópolis", "SC", "88.222-200","Rua Bonita","Lagoa",12);
MedicoDAO dao = new MedicoDAO();
dao.salvar(medico);
}
}
错误:
线程“main”javax.persistence.persistence Exception中的异常:无法执行JPA schema generation create命令[create table medico]
由Or.PrgRESQL.UT.L.PSQLExExc:错误:关系“医学”已经存在
最佳答案
我看不到图像。但试着改变
<property name="hibernate.hbm2ddl.auto">create</property> // create new schema
对此:
<property name="hibernate.hbm2ddl.auto">update</property> // update the schema