I'm new to hibernate (just started last week), and I'm developing a medical records system.
I am using Hibernate 4.3, with MySQL, and I currently have my hbm2ddl.auto property set to create-drop, although I was using update before.
My problem is, even though I only have one declaration of
private static SessionFactory sessionFactory = Connect.getSessionFactory();
and reuse this sessionFactory variable again when I'm making many inserts, it always drops by schema.
However, the hibernate documentation states that create-drop should only drop the schema when an
explicit call is made to 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();
}
}
As you can see, I haven't called sessionFactory.close(), and yet, my database is still wiped out every time. Any help would be greatly appreciated.
EDIT: Here are all of my classes that I am using in the application.
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;
}
}
Next, here's my hibernate.cfg.xml file
<?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>
Here's my patient class
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;
}
}
Finally, here's my Connect class:
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();
}
}
I know this is kind of long, but this my first post to Stack Overflow.