|
| 1 | +package org.mangorage.mangobotcore.api.util.data; |
| 2 | + |
| 3 | +import org.hibernate.Session; |
| 4 | +import org.hibernate.SessionFactory; |
| 5 | +import org.hibernate.Transaction; |
| 6 | +import org.hibernate.cfg.Configuration; |
| 7 | + |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +public final class DatabaseHandler<ID, T extends IUniqueIdHolder<ID>> { |
| 11 | + |
| 12 | + // Static factory method to create a DatabaseHandler instance |
| 13 | + public static <ID, T extends IUniqueIdHolder<ID>> DatabaseHandler<ID, T> create(String url, String user, String pass, Class<T> entityType) { |
| 14 | + return new DatabaseHandler<>(url, user, pass, entityType); |
| 15 | + } |
| 16 | + |
| 17 | + private final SessionFactory sessionFactory; |
| 18 | + private final Class<T> entityType; |
| 19 | + |
| 20 | + DatabaseHandler(String url, String user, String pass, Class<T> entityType) { |
| 21 | + this.entityType = entityType; |
| 22 | + |
| 23 | + this.sessionFactory = new Configuration() |
| 24 | + .setProperty("hibernate.connection.url", url) |
| 25 | + .setProperty("hibernate.connection.username", user) |
| 26 | + .setProperty("hibernate.connection.password", pass) |
| 27 | + .setProperty("hibernate.connection.driver_class", "com.mysql.cj.jdbc.Driver") |
| 28 | + .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect") |
| 29 | + .setProperty("hibernate.hbm2ddl.auto", "update") |
| 30 | + .setProperty("hibernate.show_sql", "false") |
| 31 | + .addAnnotatedClass(entityType) |
| 32 | + .buildSessionFactory(); |
| 33 | + } |
| 34 | + |
| 35 | + public void migrateToDatabase(List<T> fileEntities) { |
| 36 | + if (fileEntities.isEmpty()) return; |
| 37 | + |
| 38 | + try (Session session = sessionFactory.openSession()) { |
| 39 | + Transaction tx = session.beginTransaction(); |
| 40 | + |
| 41 | + for (T entity : fileEntities) { |
| 42 | + session.merge(entity); |
| 43 | + } |
| 44 | + |
| 45 | + tx.commit(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public List<T> loadEntitiesFromDatabase() { |
| 50 | + try (Session session = sessionFactory.openSession()) { |
| 51 | + return session.createQuery("from " + entityType.getName(), entityType).list(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public void saveEntity(T entity) { |
| 56 | + try (Session session = sessionFactory.openSession()) { |
| 57 | + Transaction tx = session.beginTransaction(); |
| 58 | + session.merge(entity); |
| 59 | + tx.commit(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public void removeEntity(T entity) { |
| 64 | + try (Session session = sessionFactory.openSession()) { |
| 65 | + Transaction tx = session.beginTransaction(); |
| 66 | + session.createMutationQuery("delete from " + entityType.getName() + " where id = :id") |
| 67 | + .setParameter("id", entity.getId()) |
| 68 | + .executeUpdate(); |
| 69 | + tx.commit(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public void close() { |
| 74 | + if (sessionFactory != null) { |
| 75 | + sessionFactory.close(); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments