configuring application application.xml


Application.xml is the global configuration for JavaEE.

if you use JavaEE version you can use:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd">

<module>
  <java>test-client.jar</java>
</module>

<module>
  <ejb>test-ejb.jar</ejb>
</module>

<module>
  <web>
    <web-uri>test.war</web-uri>
    <context-root>test</context-root>
  </web>
</module>

<library-directory>lib</library-directory>
</application>

If you use Java2EE you can use

<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
	<display-name>
	WarTestEAR</display-name>
	<module id="WebModule_1218106372121">
		<web>
			<web-uri>WarTest.war</web-uri>
			<context-root>WarTest</context-root>
		</web>
	</module>
</application>
<?xml version=”1.0″ encoding=”UTF-8″?>
<application id=”Application_ID” version=”1.4″ xmlns=”http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd”&gt;
<display-name>
WarTestEAR</display-name>
<module id=”WebModule_1218106372121″>
<web>
<web-uri>WarTest.war</web-uri>
<context-root>WarTest</context-root>
</web>
</module>
</application>

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);
	}