Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.alejandroszlv.mock.repository.config; import com.alejandroszlv.mock.repository.RepositoryPackage; import com.alejandroszlv.mock.repository.data.RepositoryDataPackage; import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; 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.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * * @author Alex */ @EnableTransactionManagement @EnableJpaRepositories(basePackageClasses = { RepositoryDataPackage.class }) @ComponentScan(basePackageClasses = { RepositoryPackage.class }) @Configuration public class RepositoryConfig { @Value("${db.driver}") private String dbDriver; @Value("${db.url}") private String dbURL; @Value("${db.user}") private String dbUser; @Value("${db.password}") private String dbPAssword; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); em.setPackagesToScan(new String[] { "com.alejandroszlv.mock.entity" }); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(additionalProperties()); return em; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(dbDriver); dataSource.setUrl(dbURL); dataSource.setUsername(dbUser); dataSource.setPassword(dbPAssword); return dataSource; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(emf); return transactionManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } Properties additionalProperties() { Properties properties = new Properties(); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect"); return properties; } @Bean public static PropertyPlaceholderConfigurer properties() { PropertyPlaceholderConfigurer properties = new PropertyPlaceholderConfigurer(); ClassPathResource[] resources = new ClassPathResource[] { new ClassPathResource("db.properties") }; properties.setLocations(resources); properties.setIgnoreUnresolvablePlaceholders(true); return properties; } }