use of com.voxelgameslib.voxelgameslib.internal.persistence.converter.VGLConverter in project VoxelGamesLibv2 by VoxelGamesLib.
the class HibernatePersistenceProvider method enable.
@Override
public void enable() {
boolean shouldCreateTable = config.persistence.initialTableCreation;
if (shouldCreateTable) {
config.persistence.initialTableCreation = false;
configHandler.saveGlobalConfig();
}
startupHandler.registerService("Hibernate");
Thread thread = new Thread(() -> {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySetting("hibernate.connection.username", config.persistence.user).applySetting("hibernate.connection.password", config.persistence.pass).applySetting("hibernate.connection.driver_class", config.persistence.driver).applySetting("hibernate.connection.url", config.persistence.url + "?useSSL=false").applySetting("hibernate.dialect", config.persistence.dialect).applySetting("hibernate.hbm2ddl.auto", shouldCreateTable ? "create" : "update").applySetting("hibernate.show_sql", config.persistence.showSQL + "").applySetting("hibernate.enable_lazy_load_no_trans", true).applySetting("hibernate.connection.autocommit", true).applySetting("hibernate.connection.pool_size", config.persistence.pool_size + "").build();
MetadataSources sources = new MetadataSources(registry);
try (final Timing timing = new Timing("Init converters")) {
scanner.getClassesImplementing(VGLConverter.class.getName()).loadClasses().forEach((annotatedClass) -> {
try {
((VGLConverter<?, ?>) annotatedClass.newInstance()).init();
} catch (InstantiationException | IllegalAccessException e) {
log.warning("Error while initializing converter " + annotatedClass.getSimpleName());
e.printStackTrace();
}
});
}
try (final Timing timing = new Timing("RegisterDBEntities")) {
scanner.getClassesWithAnnotation(Entity.class.getName()).loadClasses().forEach((annotatedClass) -> {
if (!annotatedClass.getName().contains("ebean"))
sources.addAnnotatedClass(annotatedClass);
});
}
try {
Metadata metadata = sources.buildMetadata();
sessionFactory = metadata.buildSessionFactory();
log.info("Build HibernationSessionFactory with " + sources.getAnnotatedClasses().size() + " entities.");
} catch (Exception e) {
StandardServiceRegistryBuilder.destroy(registry);
e.printStackTrace();
}
cBuilder = sessionFactory.getCriteriaBuilder();
startupHandler.unregisterService("Hibernate");
});
thread.setName("Hibernate Startup");
thread.start();
}
Aggregations