Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <properties>
- <!-- Persistence and Validation-->
- <hibernate.version>4.1.0.Final</hibernate.version>
- <hibernate.jpa.version>1.0.1.Final</hibernate.jpa.version>
- <javax.validation.version>1.0.0.GA</javax.validation.version>
- <querydsl.version>2.2.5</querydsl.version>
- <spring.jpa.version>1.2.0.RELEASE</spring.jpa.version>
- <spring.ldap.version>1.3.1.RELEASE</spring.ldap.version>
- <!-- Spring and Logging -->
- <spring.version>3.1.3.RELEASE</spring.version>
- <spring.security.version>3.1.3.RELEASE</spring.security.version>
- <slf4j.version>1.6.4</slf4j.version>
- <jackson.version>1.9.9</jackson.version>
- <cglib.version>3.0</cglib.version>
- </properties>
- @Bean
- public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { // Important line (notice entityManagerFactory is 'provided/autowired'
- return new JpaTransactionManager(entityManagerFactory);
- }
- @Bean
- public EntityManagerFactory getEntityManagerFactory(DataSource dataSource) { // Important line (notice dataSource is 'provided/autowired'
- LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
- factoryBean.setDataSource(dataSource);
- factoryBean.setPackagesToScan("my.scanned.domain");
- AbstractJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
- vendorAdapter.setGenerateDdl(true);
- vendorAdapter.setShowSql(false);
- vendorAdapter.setDatabase(Database.POSTGRESQL);
- vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQL82Dialect");
- factoryBean.setJpaVendorAdapter(vendorAdapter);
- Map<String, Object> properties = new HashMap<>();
- properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
- properties.put( "hibernate.bytecode.provider", "cglib" ); // Suppose to help java pergem space issues with hibernate
- factoryBean.setPersistenceProvider(new HibernatePersistence());
- factoryBean.setJpaPropertyMap(properties);
- factoryBean.setPersistenceUnitName("myPersistenace");
- factoryBean.afterPropertiesSet();
- return factoryBean.getObject(); // Important line
- }
- @Bean
- public PersistenceExceptionTranslator getHibernateExceptionTranslator() { // Required
- return new HibernateExceptionTranslator();
- }
- @Bean
- public DataSource getDataSource() {
- JndiDataSourceLookup lookup = new JndiDataSourceLookup();
- DataSource dataSource = lookup.getDataSource("java:comp/env/jdbc/myLookup");
- lookup = null;
- return dataSource;
- }
- @Configuration
- @EnableTransactionManagement
- @ImportResource( "classpath*:*jpa-repository-context.xml" )
- @ComponentScan( basePackages = { "data.repository" } )
- public class PersistenceJpaConfig
- {
- @Bean
- public LocalContainerEntityManagerFactoryBean entityManagerFactory()
- {
- LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
- factoryBean.setDataSource( dataSource() );
- factoryBean.setPackagesToScan( new String[] { "data.domain" } );
- // Setup vendor specific information. This will depend on the chosen DatabaseType
- HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
- vendorAdapter.setGenerateDdl( true );
- vendorAdapter.setShowSql( false );
- vendorAdapter.setDatabasePlatform( "org.hibernate.dialect.PostgreSQL82Dialect" );
- factoryBean.setJpaVendorAdapter( vendorAdapter );
- Map<String, Object> properties = new HashMap<String, Object>();
- properties.put( "hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy" );
- factoryBean.setJpaPropertyMap( properties );
- return factoryBean;
- }
- @Bean
- public DataSource dataSource()
- {
- JndiDataSourceLookup lookup = new JndiDataSourceLookup();
- DataSource dataSource;
- dataSource = lookup.getDataSource( "java:comp/env/jdbc/postgres" );
- return dataSource;
- }
- @Bean
- public PlatformTransactionManager transactionManager()
- {
- JpaTransactionManager transactionManager = new JpaTransactionManager();
- transactionManager.setEntityManagerFactory( entityManagerFactory().getObject() );
- return transactionManager;
- }
- }
- public interface UserAccountRepository extends JpaRepository<UserAccount, Long>, QueryDslPredicateExecutor<UserAccount> {
- }
- @Component
- public class UserAccountService {
- @Autowired
- private UserAccountRepository userAccountRepository;
- public List<UserAccount> getUserAccounts() {
- return userAccountRepository.findAll();
- }
- ...
- }
- <properties>
- <!-- Persistence and Validation-->
- <hibernate.version>4.1.0.Final</hibernate.version>
- <hibernate.jpa.version>1.0.1.Final</hibernate.jpa.version>
- <javax.validation.version>1.0.0.GA</javax.validation.version>
- <querydsl.version>2.2.5</querydsl.version>
- <spring.jpa.version>1.1.0.RELEASE</spring.jpa.version>
- <!-- Spring and Logging -->
- <spring.version>3.1.2.RELEASE</spring.version>
- <spring.security.version>3.1.2.RELEASE</spring.security.version>
- <slf4j.version>1.6.4</slf4j.version>
- <jackson.version>1.9.9</jackson.version>
- <!-- Testing Suites -->
- <selenium.version>2.24.1</selenium.version>
- </properties>
- private static final ThreadLocal<Map<Object, Object>> resources =
- new NamedThreadLocal<Map<Object, Object>>("Transactional resources");
- private static final ThreadLocal<Set<TransactionSynchronization>> synchronizations =
- new NamedThreadLocal<Set<TransactionSynchronization>>("Transaction synchronizations");
- private static final ThreadLocal<String> currentTransactionName =
- new NamedThreadLocal<String>("Current transaction name");
- private static final ThreadLocal<Boolean> currentTransactionReadOnly =
- new NamedThreadLocal<Boolean>("Current transaction read-only status");
- private static final ThreadLocal<Integer> currentTransactionIsolationLevel =
- new NamedThreadLocal<Integer>("Current transaction isolation level");
- private static final ThreadLocal<Boolean> actualTransactionActive =
- new NamedThreadLocal<Boolean>("Actual transaction active");
- TransactionSynchronizationManager.clear();
- Map<Object, Object> ojb = TransactionSynchronizationManager.getResourceMap();
- for (Object key : ojb.keySet()) {
- TransactionSynchronizationManager.unbindResource(key);
- }
- compile ('org.springframework.boot:spring-boot-starter-thymeleaf:' + bootVersion + '.RELEASE') {
- exclude module: 'spring-boot-starter-tomcat'
- exclude module: 'hibernate-validator'
- }
- // https://mvnrepository.com/artifact/org.hibernate/hibernate-validator
- compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.2.4.Final'
- group 'com.test'
- version '1.0-SNAPSHOT'
- apply plugin: 'java'
- apply plugin: 'war'
- sourceCompatibility = 1.8
- repositories {
- mavenCentral()
- }
- dependencies {
- compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '5.2.10.Final'
- compile group: 'org.springframework.data', name: 'spring-data-jpa', version: '1.11.4.RELEASE'
- compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.9.RELEASE'
- providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
- providedCompile group: 'mysql', name: 'mysql-connector-java', version: '5.1.6'
- compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
- }
- @Configuration
- @Import({JPAConfiguration.class})
- @EnableWebMvc
- public class ApplicationConfig {}
- @Configuration
- @EnableJpaRepositories("com.test.dao")
- @EnableTransactionManagement
- public class JPAConfiguration {
- @Bean
- public EntityManagerFactory entityManagerFactory() {
- LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
- factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
- factory.setPackagesToScan("com.test.model");
- factory.setDataSource(restDataSource());
- factory.setJpaPropertyMap(getPropertyMap());
- factory.afterPropertiesSet();
- return factory.getObject();
- }
- @Bean(destroyMethod = "close")
- public DataSource restDataSource() {
- BasicDataSource dataSource = new BasicDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/test");
- dataSource.setUsername("test");
- dataSource.setPassword("test");
- return dataSource;
- }
- private Map<String, String> getPropertyMap() {
- Map<String, String> hibernateProperties = new HashMap<>();
- hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
- hibernateProperties.put("hibernate.show_sql", "true");
- hibernateProperties.put("hibernate.format_sql", "true");
- hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
- return hibernateProperties;
- }
- @Bean
- public PlatformTransactionManager transactionManager() {
- JpaTransactionManager txManager = new JpaTransactionManager();
- txManager.setEntityManagerFactory(entityManagerFactory());
- return txManager;
- }
- }
- @Repository
- public interface TestRepository extends JpaRepository<TestEntity, Long> {}
- @Entity
- @Table(name = "ent")
- public class TestEntity {
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private Long id;
- private String descript;
- //equals, hashcode, toString, getters, setters
- }
- public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
- private WebApplicationContext rootContext;
- @Override
- protected Class<?>[] getRootConfigClasses() {
- return new Class[]{ApplicationConfig.class};
- }
- @Override
- protected Class<?>[] getServletConfigClasses() {
- return null;
- }
- @Override
- protected String[] getServletMappings() {
- return new String[]{"/"};
- }
- @Override
- protected WebApplicationContext createRootApplicationContext() {
- rootContext = super.createRootApplicationContext();
- return rootContext;
- }
- @Override
- public void onStartup(ServletContext servletContext) throws ServletException {
- super.onStartup(servletContext);
- AnnotationConfigWebApplicationContext restApiContext = new AnnotationConfigWebApplicationContext();
- restApiContext.setParent(rootContext);
- }
- }
- jmap -histo <tomcat_pid>
- com.test.config.dao.JPAConfiguration$$EnhancerBySpringCGLIB$$792cb231$$FastClassBySpringCGLIB$$45ff499c
- com.test.config.dao.JPAConfiguration$$FastClassBySpringCGLIB$$10104c1e
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement