import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Environment;
/**
* This wraps the hibernate SessionFactory and jams my stuff in it.
*
* @author bob
*/
public class SessionFactoryFascade {
static SessionFactory sessionFactory;
/**
* For normal access
*/
public static Session getSession4ReadOrUpdate() {
return getSession(false);
}
/**
* This drops everything in the database and creates a new schema. Used for
* reloading after changing beans (ie. modifing the schema)
*/
public static Session getSessionCreate() {
return getSession(true);
}
private static Session getSession(boolean create) {
if (sessionFactory == null) {
sessionFactory = new AnnotationConfiguration().addAnnotatedClass(
Person.class).addAnnotatedClass(Subscription.class)
.setProperty("hibernate.dialect",
"org.hibernate.dialect.MySQLDialect").setProperty(
"hibernate.connection.url", "jdbc:mysql:///test")
.setProperty("hibernate.connection.driver_class",
"com.mysql.jdbc.Driver").setProperty(
"hibernate.connection.url", "jdbc:mysql:///test")
.setProperty("hibernate.connection.username", "mysql")
.setProperty("hibernate.connection.password", "")
.setProperty(Environment.HBM2DDL_AUTO,
create ? "create" : "update").buildSessionFactory();
}
return sessionFactory.openSession();
}
}
|