Skip to content

Instantly share code, notes, and snippets.

@KupchenkoArtur
Created December 28, 2022 08:59
Show Gist options
  • Save KupchenkoArtur/6cba5e360832f2dc21778747fc03833c to your computer and use it in GitHub Desktop.
Save KupchenkoArtur/6cba5e360832f2dc21778747fc03833c to your computer and use it in GitHub Desktop.
3123
package jm.task.core.jdbc.util;
import jm.task.core.jdbc.model.User;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import java.sql.Connection;
import java.util.Properties;
public class Util {
private static final String URL = "jdbc:mysql://localhost:3306/kata";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
private static Connection connection;
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, URL);
properties.put(Environment.USER, USERNAME);
properties.put(Environment.PASS, PASSWORD);
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, "");
configuration.setProperties(properties);
configuration.addAnnotatedClass(User.class);
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.
buildSessionFactory( serviceRegistry);
} catch (HibernateException e) {
throw new RuntimeException(e);
}
}
return sessionFactory;
}
public static Connection getConnection() {
return connection;
}
// static {
// try {
//
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
//// try {
//// connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//// } catch (SQLException e) {
//// throw new RuntimeException(e);
//// }
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment