Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bg.softuni.hibernate.configuration;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.context.annotation.PropertySource;
- import org.springframework.core.env.Environment;
- import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.Database;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.transaction.PlatformTransactionManager;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import java.util.Properties;
- @Configuration
- @EnableJpaRepositories(basePackages = "bg.softuni.hibernate.repositories")
- @EnableTransactionManagement
- @PropertySource(value = "application.properties")
- public class JavaConfig {
- private final Environment environment;
- @Autowired
- public JavaConfig(Environment environment) {
- this.environment = environment;
- }
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
- driverManagerDataSource.setDriverClassName(this.environment
- .getProperty("spring.datasource.driverClassName"));
- driverManagerDataSource.setUrl(this.environment
- .getProperty("spring.datasource.url"));
- driverManagerDataSource.setUsername(this.environment
- .getProperty("spring.datasource.username"));
- driverManagerDataSource.setPassword(this.environment
- .getProperty("spring.datasource.password"));
- return driverManagerDataSource;
- }
- @Bean
- public EntityManagerFactory entityManagerFactory() {
- HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
- vendorAdapter.setDatabase(Database.MYSQL);
- vendorAdapter.setGenerateDdl(true);
- vendorAdapter.setShowSql(true);
- LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
- factory.setJpaVendorAdapter(vendorAdapter);
- factory.setPackagesToScan("bg.softuni.hibernate.entities");
- factory.setDataSource(this.dataSource());
- Properties jpaProperties = new Properties();
- jpaProperties.setProperty("hibernate.hbm2ddl.auto", "create");
- jpaProperties.setProperty("hibernate.format_sql", "true");
- factory.setJpaProperties(jpaProperties);
- factory.afterPropertiesSet();
- return factory.getObject();
- }
- @Bean
- public PlatformTransactionManager transactionManager() {
- JpaTransactionManager txManager = new JpaTransactionManager();
- txManager.setEntityManagerFactory(this.entityManagerFactory());
- return txManager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment