Created
February 2, 2023 10:17
-
-
Save KupchenkoArtur/3d0a75a9596b70a440ded6f078b1b704 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package web.config; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.PropertySource; | |
import org.springframework.core.env.Environment; | |
import org.springframework.jdbc.datasource.DriverManagerDataSource; | |
import org.springframework.orm.jpa.JpaTransactionManager; | |
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; | |
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | |
import org.springframework.transaction.PlatformTransactionManager; | |
import org.springframework.transaction.annotation.EnableTransactionManagement; | |
import javax.sql.DataSource; | |
import java.util.Objects; | |
import java.util.Properties; | |
@Configuration | |
@PropertySource("classpath:db.properties") | |
@EnableTransactionManagement | |
@ComponentScan(value = "web") | |
public class HiberConfig { | |
@Autowired | |
private Environment env; | |
@Bean | |
public DataSource getDataSource() { | |
DriverManagerDataSource dataSource = new DriverManagerDataSource(); | |
dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("db.driver"))); | |
dataSource.setUrl(env.getProperty("db.url")); | |
dataSource.setUsername(env.getProperty("db.username")); | |
dataSource.setPassword(env.getProperty("db.password")); | |
return dataSource; | |
} | |
@Bean | |
public LocalContainerEntityManagerFactoryBean getEntityManager() { | |
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); | |
Properties props=new Properties(); | |
props.put("hibernate.show_sql", env.getProperty("hibernate.show_sql")); | |
props.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); | |
factoryBean.setDataSource(getDataSource()); | |
factoryBean.setPackagesToScan(env.getProperty("db.entity.package")); | |
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); | |
factoryBean.setJpaProperties(props); | |
return factoryBean; | |
} | |
@Bean | |
public PlatformTransactionManager getPlatformTransactionManager(){ | |
JpaTransactionManager transactionManager = new JpaTransactionManager(); | |
transactionManager.setEntityManagerFactory(getEntityManager().getObject()); | |
return transactionManager; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment