Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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", ""); // 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()
} 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
You can’t perform that action at this time.