gpt4 book ai didi

java - Hibernate:hbm2ddl 属性值,创建删除总是删除架构,即使没有显式 SessionFactory 关闭?

转载 作者:行者123 更新时间:2023-11-30 00:36:59 26 4
gpt4 key购买 nike

我是 hibernate 新手(上周刚刚开始),我正在开发一个医疗记录系统。我正在使用 Hibernate 4.3 和 MySQL,目前我的 hbm2ddl.auto 属性设置为 create-drop,尽管我之前使用过 update。

我的问题是,即使我只有一个声明

private static SessionFactory sessionFactory = Connect.getSessionFactory(); 

当我进行多次插入时,再次重用此 sessionFactory 变量,它总是按模式删除。但是,hibernate 文档指出,create-drop 只应在以下情况下删除架构:显式调用 sessionFactory.close();

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class ConcreteMedicalDAO implements MedicalDAO
{
private static SessionFactory sessionFactory = Connect.getSessionFactory();

public void createDoctor(Doctor d)
{
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(d);
session.getTransaction().commit();
session.close();
}

public void retrieveDoctor(String firstName, String lastName)
{
String hql = "Select * from Doctor where firstName = :firstName and lastName = :lastName";

Session session = sessionFactory.openSession();

session.beginTransaction();
SQLQuery query = session.createSQLQuery(hql).addEntity(Doctor.class);
query.setParameter("firstName", firstName);
query.setParameter("lastName", lastName);
List<Doctor> doctors = query.list();

for(Doctor d: doctors)
{
System.out.println("Address: " + d.getAddress());
System.out.println("Birthdate: " + d.getBirthDate());
System.out.println("Degree: " + d.getDegree());
System.out.println("First name: " + d.getFirstName());
System.out.println("Gender: " + d.getGender());
System.out.println("Last name: " + d.getLastName());
System.out.println("Specialty: " + d.getSpecialty());
System.out.println();
}

session.getTransaction().commit();
session.close();
}
}

正如你所看到的,我没有调用sessionFactory.close(),但是我的数据库每次仍然被清除。任何帮助将不胜感激。

编辑:这是我在应用程序中使用的所有类。

import javax.persistence.*;

@Entity
@Table(name="doctor")
@AttributeOverrides({
@AttributeOverride(name="firstname", column=@Column(name="firstName")),
@AttributeOverride(name="lastname", column=@Column(name="lastName")),
})
public class Doctor extends Person
{
@Id
@GeneratedValue
private int dId;

@Column(name="firstName")
private String firstName;

@Column(name="lastName")
private String lastName;

@Column(name="specialty")
private String specialty;

@Column(name="degree")
private String degree;

@Column(name="gender")
private String gender;

@Column(name="Address")
private String address;

@Column(name="birthDate")
private String birthDate;

@Column(name = "userName", unique = true)
private String userName;

@Column(name="password")
private String password;

public Doctor()
{
}

public Doctor(String firstName, String lastName)
{
super(firstName, lastName);
}

public Doctor(String firstName, String lastName, String gender, String address, String birthDate, String specialty, String degree)
{
super(firstName, lastName);
this.gender = gender;
this.address = address;
this.birthDate = birthDate;
this.specialty = specialty;
this.degree = degree;
}

public int getdId()
{
return dId;
}

public void setdId(int dId)
{
this.dId = dId;
}

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getGender()
{
return gender;
}

public void setGender(String gender)
{
this.gender = gender;
}

public String getAddress()
{
return address;
}

public void setAddress(String address)
{
this.address = address;
}

public String getBirthDate()
{
return birthDate;
}

public void setBirthDate(String birthDate)
{
this.birthDate = birthDate;
}

public String getSpecialty()
{
return specialty;
}

public void setSpecialty(String specialty)
{
this.specialty = specialty;
}

public String getDegree()
{
return degree;
}

public void setDegree(String degree)
{
this.degree = degree;
}

public String getUserName()
{
return userName;
}

public void setUserName(String userName)
{
this.userName = userName;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
}

接下来,这是我的 hibernate.cfg.xml 文件

   <?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/MedicalSystem</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop the existing tables and create new one -->
<property name="hbm2ddl.auto">create-drop</property>

<!-- Mention here all the model classes along with their package name -->

<mapping class="CS157B.Doctor"/>
<mapping class="CS157B.Patient"/>
<mapping class="CS157B.MedicalRecord"/>
<mapping class="CS157B.Specialty"/>
<mapping class="CS157B.Administrator"/>
<mapping class="CS157B.Staff"/>

</session-factory>
</hibernate-configuration>

这是我的病人类别

  import javax.persistence.*;

@Entity
@Table(name="Patient")
@AttributeOverrides({
@AttributeOverride(name="firstname", column=@Column(name="firstName")),
@AttributeOverride(name="lastname", column=@Column(name="lastName")),
})
public class Patient extends Person
{
@Id
@GeneratedValue
@Column(name="pid")
private int pid;

@Column(name="firstName")
private String firstName;

@Column(name="lastName")
private String lastName;

@Column(name="gender")
private String gender;

@Column(name="birthDate")
private String birthDate;

@Column(name="address")
private String address;

@Column(name="seniorCitizen")
private boolean seniorCitizen;

@Column(name = "userName", unique = true)
private String userName;

@Column(name="password")
private String password;

@ManyToOne()
private Doctor doctor;

public Patient()
{
}

public Patient(String firstName, String lastName)
{
super(firstName, lastName);
}

public Patient(String firstName, String lastName, String gender, String address, String birthDate, boolean seniorCitizen)
{
super(firstName, lastName);
this.gender = gender;
this.address = address;
this.birthDate = birthDate;
this.seniorCitizen = seniorCitizen;
}

public int getPid()
{
return pid;
}

public void setPid(int pid)
{
this.pid = pid;
}

public String getFirstName()
{
return firstName;
}

public void setFirstName(String firstName)
{
this.firstName = firstName;
}

public String getLastName()
{
return lastName;
}

public void setLastName(String lastName)
{
this.lastName = lastName;
}

public String getGender()
{
return gender;
}

public void setGender(String gender)
{
this.gender = gender;
}

public String getBirthDate()
{
return birthDate;
}

public void setBirthDate(String birthDate)
{
this.birthDate = birthDate;
}

public String getAddress()
{
return address;
}

public void setAddress(String address)
{
this.address = address;
}

public Doctor getDoctor()
{
return doctor;
}

public void setDoctor(Doctor doctor)
{
this.doctor = doctor;
}

public boolean isSeniorCitizen()
{
return seniorCitizen;
}

public void setSeniorCitizen(boolean seniorCitizen)
{
this.seniorCitizen = seniorCitizen;
}

public String getUserName()
{
return userName;
}

public void setUserName(String userName)
{
this.userName = userName;
}

public String getPassword()
{
return password;
}

public void setPassword(String password)
{
this.password = password;
}
}

最后,这是我的 Connect 类:

   import org.hibernate.*;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class Connect
{
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

private static SessionFactory configureSessionFactory()
{
Configuration configuration = new Configuration().configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(builder.build());

return sessionFactory;
}

public static SessionFactory getSessionFactory()
{
return configureSessionFactory();
}
}

我知道这有点长,但这是我在 Stack Overflow 上的第一篇文章。

最佳答案

尝试更新您的架构

更新

而不是

创建-删除

在使用 hbm2ddl.auto 的情况下,它是创建删除的,每次部署项目时它都会删除您的架构并创建新的架构。但就 hbm2ddl.auto 而言,每当您部署项目并将数据插入数据库时​​,它都会更新您的数据库,而不是删除和创建。

关于java - Hibernate:hbm2ddl 属性值,创建删除总是删除架构,即使没有显式 SessionFactory 关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22088334/

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