Skip to content

Instantly share code, notes, and snippets.

@gitbricho
Last active August 29, 2015 14:06
Show Gist options
  • Save gitbricho/67a07438cc88eaf62f9e to your computer and use it in GitHub Desktop.
Save gitbricho/67a07438cc88eaf62f9e to your computer and use it in GitHub Desktop.
healthcare: com.itrane.healthcare.init.DbConfig
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories("com.itrane.healthcare.repo")
public class DbConfig {
@Resource
private Environment env;
// データソースの設定。
//  プロパティの値は src/resources/app.properties から取得
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getRequiredProperty("driverClassName"));
dataSource.setUrl(env.getRequiredProperty("url"));
dataSource.setUsername(env.getRequiredProperty("username"));
dataSource.setPassword(env.getRequiredProperty("password"));
return dataSource;
}
//エンティティマネージャ・ファクトリ
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
EclipseLinkJpaVendorAdapter vendorAdapter = new EclipseLinkJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(false);
LocalContainerEntityManagerFactoryBean factory =
new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(env.getRequiredProperty("model.scan.package"));
factory.setDataSource(dataSource());
//JPAプロパティの設定
Properties properties = new Properties();
appendProperties(properties, "eclipselink.ddl-generation");
//properties.put("eclipselink.ddl-generation", "update-tables");
appendProperties(properties, "eclipselink.target-database");
appendProperties(properties, "eclipselink.logging.level");
properties.put("eclipselink.deploy-on-startup", "true");
properties.put("eclipselink.ddl-generation.output-mode", "database");
properties.put("eclipselink.weaving", "static");
properties.put("eclipselink.weaving.lazy", "true");
properties.put("eclipselink.weaving.internal", "true");
properties.put("eclipselink.query-results-cache.type", "WEAK");
properties.put("eclipselink.jdbc.batch-writing", "JDBC");
properties.put("eclipselink.jdbc.batch-writing.size", "1000");
factory.setJpaProperties(properties);
return factory;
}
private void appendProperties(Properties p, String key) {
p.put(key, env.getRequiredProperty(key));
}
//トランザクションマネージャ
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
// JpaDialect
@Bean
public EclipseLinkJpaDialect eclipseLinkJpaDialect() {
return new EclipseLinkJpaDialect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment