Skip to content

Instantly share code, notes, and snippets.

@nunenuh
Created April 17, 2015 18:31
Show Gist options
  • Save nunenuh/686d8f8315091043c263 to your computer and use it in GitHub Desktop.
Save nunenuh/686d8f8315091043c263 to your computer and use it in GitHub Desktop.
HIBERNATE UTIL BARU
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package epsetupserv.orm.utils;
import epsetupserv.config.GeneralConfigurator;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author nunenuh
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
//preparing for hibernate setup auth
String path = System.getProperty("user.dir") + "/conf/hibernate/";
String propFile = "hibernate.cfg.properties";
String mapFile = "hibernate.mapping.properties";
AnnotationConfiguration ac = new AnnotationConfiguration();;
File f = new File(path.concat(propFile));
FileInputStream fis = new FileInputStream(f);
ac.getProperties().load(fis);
GeneralConfigurator gc = new GeneralConfigurator(path, mapFile);
List<String> list = gc.getProperties("mapping.resources");
System.out.println("Hibernate Mapping Size = "+list.size());
for (Iterator<String> it = list.iterator(); it.hasNext();) {
String xmlFile = it.next();
ac.addFile(path.concat(xmlFile));
System.out.println("HIbernate Add Mapping Path = " + path.concat(xmlFile));
}
sessionFactory = ac.buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Exception stack Trace ************** begin");
System.err.println("Hibernate : Initial SessionFactory creation failed." + ex);
ex.printStackTrace();
System.err.println("Exception Stack Trace ********* END");
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment