diff options
author | mrfoxygmfr <mrfoxygmfr@sch9.ru> | 2025-03-17 12:23:11 +0300 |
---|---|---|
committer | mrfoxygmfr <mrfoxygmfr@sch9.ru> | 2025-03-17 12:23:11 +0300 |
commit | ef2bec524581a63d989fc3de70433a118bd905fa (patch) | |
tree | abd166a5f65fd7c746bf216680bf8916bb4503a3 | |
parent | fef1dc61af7373892eb32595ddc47329c2167d78 (diff) |
feat(db): add hibernate session factory
-rw-r--r-- | src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/config/HibernateConfig.java | 49 | ||||
-rw-r--r-- | src/main/resources/application.properties | 4 |
2 files changed, 53 insertions, 0 deletions
diff --git a/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/config/HibernateConfig.java b/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/config/HibernateConfig.java new file mode 100644 index 0000000..7f7948a --- /dev/null +++ b/src/main/java/ru/mrfoxygmfr/warehouse_accounting/db/config/HibernateConfig.java @@ -0,0 +1,49 @@ +package ru.mrfoxygmfr.warehouse_accounting.db.config; + +import org.springframework.context.annotation.*; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; + +import javax.sql.DataSource; +import java.util.Properties; + +@Configuration +@PropertySource("classpath:application.properties") +public class HibernateConfig { + @Value("${database.driver}") + private String DB_DRIVER; + @Value("${database.url}") + private String DB_URL; + @Value("${database.username}") + private String DB_USERNAME; + @Value("${database.password}") + private String DB_PASSWORD; + + @Bean(name = "entityManagerFactory") + public LocalSessionFactoryBean sessionFactory() { + LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); + sessionFactory.setDataSource(oraDataSource()); + sessionFactory.setPackagesToScan("ru.mrfoxygmfr.warehouse_accounting.db.models"); + + Properties hibernateProperties = new Properties(); + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update"); + hibernateProperties.setProperty("connection_pool_size", "1"); + + sessionFactory.setHibernateProperties(hibernateProperties); + + return sessionFactory; + } + + @Bean + public DataSource oraDataSource() { + DriverManagerDataSource dataSource = new DriverManagerDataSource(); + + dataSource.setDriverClassName(DB_DRIVER); + dataSource.setUrl(DB_URL); + dataSource.setUsername(DB_USERNAME); + dataSource.setPassword(DB_PASSWORD); + + return dataSource; + } +}
\ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index e711a88..f1d166d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,6 @@ spring.application.name=warehouse_accounting +database.driver=org.postgresql.Driver +database.url=jdbc:postgresql://localhost:5432/warehouse_accounting +database.username=postgres +database.password=postgres |