0

I have next working code with ddl and dml. Its initialize embedded H2 database.

    @Bean
    public DataSource dataSource(@Value("${jdbc.driver}") String driver,
                             @Value("${jdbc.url}") String url,
                             @Value("${jdbc.user}") String user,
                             @Value("${jdbc.password}") String password) {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(user);
    dataSource.setPassword(password);

    Resource initSchema = new ClassPathResource("schema.sql");
    Resource initData = new ClassPathResource("data.sql");
    DatabasePopulator databasePopulator = new ResourceDatabasePopulator(initSchema, initData);
    DatabasePopulatorUtils.execute(databasePopulator, dataSource);

    return dataSource;
}
    @Bean
    public EntityManagerFactory entityManagerFactory(DataSource dataSource) {
    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(false);
    jpaVendorAdapter.setShowSql(true);

    LocalContainerEntityManagerFactoryBean entityManagerFactory =
            new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setDataSource(dataSource);
    entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
    entityManagerFactory.setPackagesToScan("org.doit.model");
    entityManagerFactory.afterPropertiesSet();
    return entityManagerFactory.getObject();
}

@Bean
public JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory);
    return transactionManager;
}

Pls give me advise, how to insert in database the binary data, like a zip file that is located in the resource folder. I know that in unit test we can use annotation @Before which give us opportunity to do what you want. But how do it when you start your app with java config.

1 Answer 1

1

I have also faced the same issue. This answer worked for me. You can try it, I think this will work for you.

Sign up to request clarification or add additional context in comments.

1 Comment

Very strange sql script will be) If file will be more than 100 bytes its unreal to write scripts)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.