Get SessionFactory Instance without using hibernate.cfg.xml


Database Config

package co.id.hanoman.btnmw.spp.conf;

import java.util.List;
import java.util.Properties;
import java.util.Vector;

public class DatabaseConfig {
	private static String MYSQL_DBMS = "MYSQL";
	private static String SQLSERVER_DBMS = "SQLServer";

	private static String SQLSERVER_DRIVER = "net.sourceforge.jtds.jdbc.Driver";
	private static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";

	private String host;
	private String port;
	private String dbname;
	private String dbms;
	private String user;
	private String pass;
	private List<Class<?>> mapList;
	private Properties properties;

	public DatabaseConfig(String host, String port, String dbname, String dbms,
			String user, String pass) {
		mapList = new Vector<Class<?>>();
		properties = new Properties();
		this.host = host;
		this.port = port;
		this.dbname = dbname;
		this.dbms = dbms;
		this.user = user;
		this.pass = pass;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public String getDbms() {
		return dbms;
	}

	public void setDbms(String dbms) {
		this.dbms = dbms;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public List<Class<?>> getAnnotatedClass() {
		return mapList;
	}

	public void setMapList(List<Class<?>> mapList) {
		this.mapList = mapList;
	}

	public String getDriver() {

		if (dbms.equalsIgnoreCase(MYSQL_DBMS)) {
			return MYSQL_DRIVER;
		} else if (dbms.equalsIgnoreCase(SQLSERVER_DBMS)) {
			return SQLSERVER_DRIVER;
		}

		return null;
	}

	public String getUrl() {
		System.out.println(dbms + MYSQL_DBMS);
		if (dbms.equalsIgnoreCase(MYSQL_DBMS)) {
			return String.format("jdbc:mysql://%s:%s/%s", host, port, dbname);
		} else if (dbms.equalsIgnoreCase(SQLSERVER_DBMS)) {
			return String.format("jdbc:jtds:sqlserver://%s:%s/%s", host, port,
					dbname);
		}

		return null;
	}

	public String getDialect() {
		if (dbms.equalsIgnoreCase(MYSQL_DBMS)) {
			return "org.hibernate.dialect.SQLServerDialect";
		} else if (dbms.equalsIgnoreCase(SQLSERVER_DBMS)) {
			return "org.hibernate.dialect.MySQLDialect";
		}

		return null;
	}

	public void addAnnotatedClass(Class<?> entity) {
		mapList.add(entity);
	}

	public void removeAnnotatedClass(Class<?> entity) {
		mapList.remove(entity);
	}
	
	public Properties getProperties() {
		return properties;
	}
	
	public void addProperty(String k, String v) {
		properties.setProperty(k, v);
	}
	
	public void removeProperty(String k) {
		properties.remove(k);
	}
}

Syncrhonizer Session Factory

package co.id.hanoman.btnmw.spp.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

import co.id.hanoman.btnmw.spp.conf.DatabaseConfig;
import co.id.hanoman.btnmw.spp.entity.Pembayaran;
import co.id.hanoman.btnmw.spp.entity.Tagihan;


public class SynchronizerSessionFactory {
	private static Logger logger = Logger
			.getLogger(SynchronizerSessionFactory.class);

	public static SessionFactory getSessionFactory(DatabaseConfig conf) {
		logger.debug("getSessionFactory invoked");
		
		try {
			conf.addAnnotatedClass(Pembayaran.class);
			conf.addAnnotatedClass(Tagihan.class);
			conf.addProperty("hibernate.hbm2ddl.auto", "update");
			return HibernateSessionFactory.getSessionFactory(conf);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			throw e;
		}
	}

}

HibernateSessionFactory

package co.id.hanoman.btnmw.spp.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;

import co.id.hanoman.btnmw.spp.conf.DatabaseConfig;
import co.id.hanoman.btnmw.spp.entity.Pembayaran;
import co.id.hanoman.btnmw.spp.entity.Tagihan;


public class SynchronizerSessionFactory {
	private static Logger logger = Logger
			.getLogger(SynchronizerSessionFactory.class);

	public static SessionFactory getSessionFactory(DatabaseConfig conf) {
		logger.debug("getSessionFactory invoked");
		
		try {
			conf.addAnnotatedClass(Pembayaran.class);
			conf.addAnnotatedClass(Tagihan.class);
			conf.addProperty("hibernate.hbm2ddl.auto", "update");
			return HibernateSessionFactory.getSessionFactory(conf);
		} catch (HibernateException e) {
			logger.error(e.getMessage(), e);
			throw e;
		}
	}

}

@Test
	public void testGetSessionFactory() {
		DatabaseConfig conf = new DatabaseConfig("localhost", "3306", "btn_usu", "mysql", "root", "root");
		conf.addAnnotatedClass(Tagihan.class);
		conf.addAnnotatedClass(Pembayaran.class);
		conf.addProperty("hibernate.hbm2ddl.auto", "create");
		sf = SynchronizerSessionFactory.getSessionFactory(conf);
		assertNotNull(sf);
		
		ITagihanDao tagihanDao = new TagihanDao(sf);
		tagihanDao.save(tagihan);
		
		IPembayaranDao pembayaranDao = new PembayaranDao(sf);
		pembayaranDao.save(pembayaran);
	}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s