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.chevrier.legiondao.config; import javax.persistence.EntityManagerFactory; import org.apache.tomcat.jdbc.pool.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.ServletRegistrationBean; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; import org.springframework.boot.orm.jpa.EntityScan; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; 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 org.springframework.web.servlet.DispatcherServlet; import org.springframework.web.servlet.config.annotation.EnableWebMvc; /** *j'aime suzy, c'est la plus belle! * @author nicolas */ //@EnableWebMvc @EnableAutoConfiguration @EnableJpaRepositories(basePackages = { "com.chevrier.legiondao.repository" }) @ComponentScan(basePackages = { "com.chevrier.legiondao.metier" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Configuration.class) }) @EntityScan(basePackages = { "com.chevrier.legiondao.entities" }) @EnableTransactionManagement public class ConfigJpa { @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter(); hibernateJpaVendorAdapter.setShowSql(true); hibernateJpaVendorAdapter.setGenerateDdl(true); hibernateJpaVendorAdapter.setDatabase(Database.MYSQL); //hibernateJpaVendorAdapter.getJpaPropertyMap().put("hibernate.naming-strategy", ConfigJdbc.DEFAULT_NAMING_STRATEGY); return hibernateJpaVendorAdapter; } // packages des entits JPA public final static String[] ENTITIES_PACKAGES = { "com.chevrier.legiondao.entities" }; // source de donnes @Bean public DataSource dataSource() { // source de donnes TomcatJdbc DataSource dataSource = new DataSource(); // configuration accs JDBC dataSource.setDriverClassName(ConfigJdbc.DRIVER_CLASSNAME); dataSource.setUsername(ConfigJdbc.USER_DBPRODUITSCATEGORIES); dataSource.setPassword(ConfigJdbc.PASSWD_DBPRODUITSCATEGORIES); dataSource.setUrl(ConfigJdbc.URL_DBPRODUITSCATEGORIES); // connexions ouvertes initialement dataSource.setInitialSize(5); // rsultat /*return new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseType.HSQL) .addScript("classpath:sql/schema.sql") .addScript("classpath:sql/test-data.sql") .build();*/ return dataSource; } // EntityManagerFactory /*@Bean public EntityManagerFactory entityManagerFactory(JpaVendorAdapter jpaVendorAdapter, DataSource dataSource) { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(jpaVendorAdapter); //Map<String, Object> mapJpaProperties = factory.getJpaPropertyMap(); //mapJpaProperties.put("openjpa.jdbc.MappingDefaults", //"ForeignKeyDeleteAction=cascade,JoinForeignKeyDeleteAction=restrict"); factory.setDataSource(dataSource); factory.afterPropertiesSet(); 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 properties.put("hibernate.batch_fetch_style", "PADDED"); // Fix problem with selecting from a base repository for all instances of super types: https://hibernate.atlassian.net/browse/HHH-8830 factoryBean.setJpaPropertyMap(properties); return factory.getObject(); }*/ // Transaction manager @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory); return txManager; } /*@Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory() { return new TomcatEmbeddedServletContainerFactory("", 8080); }*/ }