Skip to content

Commit f86afde

Browse files
committed
Implemented a DatabaseHandler class
1 parent c392487 commit f86afde

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ dependencies {
9090
strictly("26.0.2")
9191
}
9292
}
93+
94+
// SQL Stuff
95+
library("org.hibernate.orm:hibernate-core:6.4.4.Final")
96+
library("jakarta.persistence:jakarta.persistence-api:3.1.0")
97+
library("jakarta.interceptor:jakarta.interceptor-api:2.1.0")
98+
library('jakarta.enterprise:jakarta.enterprise.cdi-api:4.0.1')
99+
100+
// SQL Driver
101+
library("com.mysql:mysql-connector-j:8.4.0")
93102
}
94103

95104
tasks.named('processResources', ProcessResources).configure {

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
requires static org.spongepowered.mixin;
1818

1919
requires com.google.gson;
20+
requires org.hibernate.orm.core;
2021

2122
// Config API
2223
exports org.mangorage.mangobotcore.api.config.v1;
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.mangorage.mangobotcore.api.util.data;
2+
3+
public interface IUniqueIdHolder<T> {
4+
T getId();
5+
}

0 commit comments

Comments
 (0)