Skip to content

Instantly share code, notes, and snippets.

@AhmedYousseff
Created March 13, 2020 20:58
Show Gist options
  • Save AhmedYousseff/adeed8556699c2c402878a8ba9e2f099 to your computer and use it in GitHub Desktop.
Save AhmedYousseff/adeed8556699c2c402878a8ba9e2f099 to your computer and use it in GitHub Desktop.
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
Properties properties = new Properties();
properties.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
properties.put(Environment.URL, "jdbc:mysql://localhost:3306/medium?useSSL=false");
properties.put(Environment.USER, "root");
properties.put(Environment.PASS, "");
properties.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
properties.put(Environment.SHOW_SQL, "true");
properties.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
properties.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(properties);
configuration.addAnnotatedClass(Car.class);
ServiceRegistry registry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();
sessionFactory = configuration.buildSessionFactory(registry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment