B
B
Borlandfcsd2018-08-24 14:05:36
Java
Borlandfcsd, 2018-08-24 14:05:36

How to execute sql script at project start?

How to properly execute sql script using spring data and hibernate?

tried to prescribe

property:

spoiler
#DB properties:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://localhost:5432/cinema
db.username=postgres
db.password=postgres

#Hibernate Configuration:
db.hibernate.dialect=org.hibernate.dialect.PostgreSQL82Dialect
db.hibernate.show_sql=true
db.entitymanager.packages.to.scan=io.borlandfcsd.cinema.model.entity
db.hibernate.hbm2ddl-auto = create
db.hibernate.hbm2ddl.import-files = /db/create.sql,/db/load.sql


and datasource
spoiler

@Configuration
@EnableTransactionManagement
@ComponentScan("io.borlandfcsd.cinema")
@PropertySource("classpath:app.properties")
@EnableJpaRepositories("io.borlandfcsd.cinema.model.repository")
public class DataConfig {

    private static final String PROP_DATABASE_DRIVER = "db.driver";
    private static final String PROP_DATABASE_PASSWORD = "db.password";
    private static final String PROP_DATABASE_URL = "db.url";
    private static final String PROP_DATABASE_USERNAME = "db.username";
    private static final String PROP_HIBERNATE_DIALECT = "db.hibernate.dialect";
    private static final String PROP_HIBERNATE_SHOW_SQL = "db.hibernate.show_sql";
    private static final String PROP_ENTITYMANAGER_PACKAGES_TO_SCAN = "db.entitymanager.packages.to.scan";
    private static final String PROP_HIBERNATE_HBM2DDL_AUTO = "db.hibernate.hbm2ddl-auto";
    private static final String PROP_HIBERNATE_HBM2DDL_IMPORT_FILES = "db.hibernate.hbm2ddl.import-files";



    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(env.getRequiredProperty(PROP_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROP_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROP_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROP_DATABASE_PASSWORD));

        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setDatabase(org.springframework.orm.jpa.vendor.Database.POSTGRESQL);
        vendorAdapter.setGenerateDdl(true);

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(env.getRequiredProperty(PROP_ENTITYMANAGER_PACKAGES_TO_SCAN));

        entityManagerFactoryBean.setJpaProperties(getHibernateProperties());

        return entityManagerFactoryBean;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

        return transactionManager;
    }

    private Properties getHibernateProperties() {
        Properties properties = new Properties();
        properties.put(PROP_HIBERNATE_DIALECT, env.getRequiredProperty(PROP_HIBERNATE_DIALECT));
        properties.put(PROP_HIBERNATE_SHOW_SQL, env.getRequiredProperty(PROP_HIBERNATE_SHOW_SQL));
        properties.put(PROP_HIBERNATE_HBM2DDL_AUTO, env.getRequiredProperty(PROP_HIBERNATE_HBM2DDL_AUTO));
        properties.put(PROP_HIBERNATE_HBM2DDL_IMPORT_FILES, env.getRequiredProperty(PROP_HIBERNATE_HBM2DDL_IMPORT_FILES));

        return properties;
    }

}



log
spoiler

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See www.slf4j.org/codes.html#StaticLoggerBinder for further details.
авг 24, 2018 1:59:50 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: default
...]
авг 24, 2018 1:59:50 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.3.Final}
авг 24, 2018 1:59:50 PM org.hibernate.cfg.Environment
INFO: HHH000206: hibernate.properties not found
авг 24, 2018 1:59:51 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
авг 24, 2018 1:59:51 PM org.hibernate.dialect.Dialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
авг 24, 2018 1:59:52 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
авг 24, 2018 1:59:52 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : [email protected]
авг 24, 2018 1:59:55 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alfss, 2018-08-25
@alfss

liquibase, flyway

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question