Search in sources :

Example 1 with VGLConverter

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();
}
Also used : Entity(javax.persistence.Entity) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) VGLConverter(com.voxelgameslib.voxelgameslib.internal.persistence.converter.VGLConverter) MetadataSources(org.hibernate.boot.MetadataSources) Metadata(org.hibernate.boot.Metadata) JDBCConnectionException(org.hibernate.exception.JDBCConnectionException) Timing(com.voxelgameslib.voxelgameslib.internal.timings.Timing) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry)

Aggregations

VGLConverter (com.voxelgameslib.voxelgameslib.internal.persistence.converter.VGLConverter)1 Timing (com.voxelgameslib.voxelgameslib.internal.timings.Timing)1 Entity (javax.persistence.Entity)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 JDBCConnectionException (org.hibernate.exception.JDBCConnectionException)1