gpt4 book ai didi

java - hibernate JAVA |如何在不创建新表的情况下获取 EntityManager 的实例?

转载 作者:行者123 更新时间:2023-11-29 14:18:11 26 4
gpt4 key购买 nike

每次我想要获取 Entity Manager 实例时都会在数据库中创建新表,那么如何在不创建新表的情况下在 Postgresql 中获取 EntityManager(插入、删除、更新数据库)?

实体“Medico”:

@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
}

“MedicoDAO”

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","roberto@medico.com","12345","48 3275 0463", "Florianópolis", "SC", "88.222-200","Rua Bonita","Lagoa",12);

MedicoDAO dao = new MedicoDAO();
dao.salvar(medico);
}
}

错误:

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

最佳答案

我看不到图像。但试着改变

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

为此:

<property name="hibernate.hbm2ddl.auto">update</property> // update the schema

关于java - hibernate JAVA |如何在不创建新表的情况下获取 EntityManager 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40754271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com