Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- @ComponentScan(basePackages = "com.evgenltd.levit.core")
- @EnableTransactionManagement
- public class Config {
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
- final Map<String,Object> properties = new HashMap<>();
- properties.put("hibernate.hbm2ddl.auto","none");
- properties.put("hibernate.dialect","org.hibernate.dialect.SQLiteDialect");
- properties.put("hibernate.show_sql","false");
- properties.put("hibernate.connection.foreign_keys","true");
- properties.put("hibernate.connection.autocommit","false");
- final JpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
- final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
- entityManagerFactory.setDataSource(dataSource());
- entityManagerFactory.setPackagesToScan("com.evgenltd.levit.core.entity");
- entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter);
- entityManagerFactory.setJpaPropertyMap(properties);
- return entityManagerFactory;
- }
- @Bean
- public SingleConnectionDataSource dataSource() {
- try {
- Class.forName("org.sqlite.JDBC");
- final Connection connection = DriverManager.getConnection("jdbc:sqlite:data.db");
- connection.setAutoCommit(false);
- final Statement statement = connection.createStatement();
- statement.executeUpdate("pragma foreign_keys = on");
- statement.close();
- return new SingleConnectionDataSource(connection, true);
- }catch(Exception e) {
- throw new RuntimeException(e);
- }
- }
- @Bean
- public PlatformTransactionManager transactionManager(final EntityManagerFactory entityManagerFactory) {
- final JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
- jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
- return jpaTransactionManager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement