Skip to content

Instantly share code, notes, and snippets.

@SakaDream
Created August 27, 2017 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SakaDream/80ff2f1d02336dd36e890c9164a215a2 to your computer and use it in GitHub Desktop.
Save SakaDream/80ff2f1d02336dd36e890c9164a215a2 to your computer and use it in GitHub Desktop.
package com.sakadream;
import com.sakadream.models.Employee;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import java.util.Properties;
/**
* Created by Phan Ba Hai on 25/08/2017.
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Properties prop = new Properties();
prop.put("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver"); // Thay Driver class tuỳ theo CSDL bạn đang sử dụng
prop.put("hibernate.connection.url", "YOUR_CONNECTION_URL");
prop.put("hibernate.connection.username", "YOUR_DATABASE_USERNAME");
prop.put("hibernate.connection.password", "YOUR_DATABASE_PASSWORD");
prop.put("hibernate.current_session_context_class", "thread");
prop.put("hibernate.query.factory_class", "org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory");
prop.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect");
prop.put("hibernate.show_sql", "true");
sessionFactory = new AnnotationConfiguration()
.addPackage("YOUR_MODEL_PACKAGE")
.addProperties(prop)
.addAnnotatedClass(Employee.class)
.buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
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