feat(db): Add hibernate and SQLite
This commit is contained in:
parent
566336b569
commit
3ab16e6303
@ -1,5 +1,6 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'com.github.johnrengelman.shadow' version '7.1.2'
|
||||
}
|
||||
|
||||
group = 'de.polyfish0'
|
||||
@ -18,15 +19,20 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(platform("org.hibernate.orm:hibernate-platform:6.6.7.Final"))
|
||||
|
||||
compileOnly("org.spigotmc:spigot-api:1.21.3-R0.1-SNAPSHOT")
|
||||
|
||||
compileOnly 'org.projectlombok:lombok:1.18.36'
|
||||
compileOnly('org.projectlombok:lombok:1.18.36')
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.36'
|
||||
|
||||
testCompileOnly 'org.projectlombok:lombok:1.18.36'
|
||||
testAnnotationProcessor 'org.projectlombok:lombok:1.18.36'
|
||||
|
||||
implementation("org.xerial:sqlite-jdbc:3.48.0.0")
|
||||
implementation("org.hibernate.orm:hibernate-core:6.6.7.Final")
|
||||
implementation("jakarta.transaction:jakarta.transaction-api")
|
||||
implementation("org.hibernate.orm:hibernate-community-dialects:6.6.7.Final")
|
||||
}
|
||||
|
||||
def targetJavaVersion = 21
|
||||
|
@ -4,10 +4,15 @@ import com.google.common.reflect.ClassPath;
|
||||
import de.polyfish0.survivalGames.annotations.Command;
|
||||
import de.polyfish0.survivalGames.annotations.EventClass;
|
||||
import de.polyfish0.survivalGames.annotations.Permission;
|
||||
import de.polyfish0.survivalGames.database.HibernateUtil;
|
||||
import de.polyfish0.survivalGames.database.player.PlayerEntity;
|
||||
import de.polyfish0.survivalGames.database.player.PlayerRepository;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashSet;
|
||||
@ -24,6 +29,22 @@ public final class SurvivalGames extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
plugin = this;
|
||||
|
||||
File folder = new File(String.format("plugins/%s", getDescription().getName()));
|
||||
|
||||
if(!folder.isDirectory())
|
||||
folder.delete();
|
||||
|
||||
if(!folder.exists())
|
||||
folder.mkdirs();
|
||||
|
||||
/*PlayerEntity player = new PlayerEntity(0L, "Mika");
|
||||
PlayerRepository playerRepository = new PlayerRepository();
|
||||
playerRepository.save(player).thenRun(() -> {
|
||||
Bukkit.getScheduler().runTask(plugin, () -> {
|
||||
Bukkit.broadcastMessage("Spieler Gepseichert!");
|
||||
});
|
||||
});*/
|
||||
|
||||
try {
|
||||
ClassPath cp = ClassPath.from(getClass().getClassLoader());
|
||||
|
||||
@ -41,6 +62,11 @@ public final class SurvivalGames extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
HibernateUtil.shutdown();
|
||||
}
|
||||
|
||||
private void registerCommand(Class<?> klass) {
|
||||
if(!CommandExecutor.class.isAssignableFrom(klass)) {
|
||||
logger.log(Level.SEVERE, "Class \"" + klass.getName() + "\" is annotated with @Command but does not implements CommandExecutor");
|
||||
|
@ -0,0 +1,47 @@
|
||||
package de.polyfish0.survivalGames.database;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public abstract class BaseRepository<T> {
|
||||
private final Class<T> clazz;
|
||||
|
||||
public BaseRepository(Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> save(Object entity) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try (Session session = HibernateUtil.getSession()) {
|
||||
Transaction tx = session.beginTransaction();
|
||||
session.persist(entity);
|
||||
tx.commit();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public <T> CompletableFuture<T> findById(Class<T> clazz, Long id) {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
try (Session session = HibernateUtil.getSession()) {
|
||||
return session.get(clazz, id);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> delete(T entity) {
|
||||
return CompletableFuture.runAsync(() -> {
|
||||
try (Session session = HibernateUtil.getSession()) {
|
||||
Transaction tx = session.beginTransaction();
|
||||
session.remove(entity);
|
||||
tx.commit();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package de.polyfish0.survivalGames.database;
|
||||
|
||||
import de.polyfish0.survivalGames.SurvivalGames;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class Database {
|
||||
@Getter
|
||||
private static Database instance = new Database();
|
||||
|
||||
private Connection manager;
|
||||
|
||||
public void connect() {
|
||||
try {
|
||||
manager = DriverManager.getConnection(String.format("jdbc:sqlite:plugins/%s/data.db", SurvivalGames.plugin.getDescription().getName()));
|
||||
if(manager == null)
|
||||
throw new SQLException("Manager is null");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
Bukkit.getPluginManager().disablePlugin(SurvivalGames.plugin);
|
||||
}
|
||||
}
|
||||
|
||||
public void createTables() {
|
||||
manager.createStatement("")
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package de.polyfish0.survivalGames.database;
|
||||
|
||||
import de.polyfish0.survivalGames.SurvivalGames;
|
||||
import de.polyfish0.survivalGames.database.player.PlayerEntity;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
|
||||
public class HibernateUtil {
|
||||
private static final SessionFactory sessionFactory = new Configuration()
|
||||
.setProperty("hibernate.connection.driver_class", "org.sqlite.JDBC")
|
||||
.setProperty("hibernate.connection.url", String.format("jdbc:sqlite:plugins/%s/database.db", SurvivalGames.plugin.getDescription().getName()))
|
||||
.setProperty("hibernate.dialect", "org.hibernate.community.dialect.SQLiteDialect")
|
||||
.setProperty("hibernate.hbm2ddl.auto", "create")
|
||||
.setProperty("hibernate.show_sql", "true")
|
||||
.addAnnotatedClass(PlayerEntity.class)
|
||||
.buildSessionFactory();
|
||||
|
||||
public static Session getSession() {
|
||||
return sessionFactory.openSession();
|
||||
}
|
||||
|
||||
public static void shutdown() {
|
||||
sessionFactory.close();
|
||||
}
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
package de.polyfish0.survivalGames.database;
|
||||
|
||||
public interface Table {
|
||||
void setup();
|
||||
void update();
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package de.polyfish0.survivalGames.database.player;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Entity
|
||||
@Table
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PlayerEntity {
|
||||
@Id
|
||||
private Long id;
|
||||
private String name;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package de.polyfish0.survivalGames.database.player;
|
||||
|
||||
import de.polyfish0.survivalGames.database.BaseRepository;
|
||||
|
||||
public class PlayerRepository extends BaseRepository<PlayerEntity> {
|
||||
public PlayerRepository() {
|
||||
super(PlayerEntity.class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user