Hibernate:hbm2ddl属性值,create-drop总是丢弃模式,即使没有显式的SessionFactory关闭?

时间:2014-02-28 06:56:51

标签: java mysql hibernate

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

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

的声明
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上的第一篇文章。

1 个答案:

答案 0 :(得分:0)

尝试更新您的架构

更新

而不是

创建降

对于使用hbm2ddl.auto的情况是create-drop,每次在部署项目时都会删除模式并创建新模式。但是对于hbm2ddl.auto,无论何时将部署项目并将数据投入到数据库中,它都会更新数据库而不是删除并创建。