Advertisement
SimonAnkov

Untitled

Apr 9th, 2025
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import jakarta.persistence.EntityManagerFactory;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.nio.file.Files;
  5. import java.nio.file.StandardCopyOption;
  6. import java.util.HashMap;
  7. import javax.sql.DataSource;
  8. import org.springframework.beans.factory.annotation.Qualifier;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.Configuration;
  13. import org.springframework.core.io.Resource;
  14. import org.springframework.core.io.ResourceLoader;
  15. import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  16. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  17. import org.springframework.orm.jpa.JpaTransactionManager;
  18. import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  19. import org.springframework.transaction.PlatformTransactionManager;
  20.  
  21. @Configuration
  22. @EnableJpaRepositories(
  23.         basePackages = "com.template.plex.repository.sqlite",
  24.         entityManagerFactoryRef = "sqliteEntityManagerFactory",
  25.         transactionManagerRef = "sqliteTransactionManager"
  26. )
  27. public class SqliteConfig {
  28.  
  29.     @Bean(name = "sqliteDataSource")
  30.     public DataSource sqliteDataSource(ResourceLoader resourceLoader,
  31.             @Value("${spring.datasource.sqlite.dbName}") String dbFileName) {
  32.         DriverManagerDataSource dataSource = new DriverManagerDataSource();
  33.         dataSource.setDriverClassName("org.sqlite.JDBC");
  34.  
  35.         String resourcePath = "classpath:db/sqlite/" + dbFileName;
  36.         Resource sqliteResource = resourceLoader.getResource(resourcePath);
  37.  
  38.         if (!sqliteResource.exists()) {
  39.             throw new IllegalArgumentException("SQLite DB file " + dbFileName
  40.                     + " not found in resources (expected at " + resourcePath + ")");
  41.         }
  42.  
  43.         File tempDbFile;
  44.         try {
  45.             tempDbFile = File.createTempFile("sqlite-migration-", ".db");
  46.             Files.copy(sqliteResource.getInputStream(), tempDbFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
  47.         } catch (IOException e) {
  48.             throw new RuntimeException("Failed to create temporary SQLite DB file", e);
  49.         }
  50.  
  51.         dataSource.setUrl("jdbc:sqlite:" + tempDbFile.getAbsolutePath());
  52.         return dataSource;
  53.     }
  54.  
  55.     @Bean(name = "sqliteEntityManagerFactory")
  56.     public LocalContainerEntityManagerFactoryBean sqliteEntityManagerFactory(
  57.             EntityManagerFactoryBuilder builder,
  58.             @Qualifier("sqliteDataSource") DataSource dataSource) {
  59.  
  60.         HashMap<String, Object> jpaProps = new HashMap<>();
  61.         jpaProps.put("hibernate.hbm2ddl.auto", "none");
  62.         jpaProps.put("hibernate.default_schema", "");
  63.         jpaProps.put("hibernate.dialect", "org.hibernate.community.dialect.SQLiteDialect");
  64.  
  65.         return builder
  66.                 .dataSource(dataSource)
  67.                 .packages("com.template.plex.model.external")
  68.                 .persistenceUnit("sqlite")
  69.                 .properties(jpaProps)
  70.                 .build();
  71.     }
  72.  
  73.     @Bean(name = {"sqliteTransactionManager", "transactionManager"})
  74.     public PlatformTransactionManager sqliteTransactionManager(
  75.             @Qualifier("sqliteEntityManagerFactory") EntityManagerFactory emf) {
  76.         return new JpaTransactionManager(emf);
  77.     }
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement