我是冬眠的新手(刚从上周开始),并且正在开发医疗记录系统。
我正在将Hibernate 4.3和MySQL一起使用,尽管我以前使用过update,但我目前已将hbm2ddl.auto属性设置为create-drop。

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

private static SessionFactory sessionFactory = Connect.getSessionFactory();


并在我进行多次插入时再次重用此sessionFactory变量,它总是按模式删除。
但是,休眠文档指出,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是create-drop,则每次部署架构时,它将删除您的架构并创建一个新架构。但是对于hbm2ddl.auto,无论何时将项目部署并将数据保险到数据库中,它都会更新数据库,而不是通过拖放创建。

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

10-11 17:34