Java tutorial
/** * Copyright 2016 REPLACE ME OWNER (REPLACE ME YEAR) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.homiefund.config; import org.flywaydb.core.Flyway; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.core.env.Environment; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; /** * Created by Dominik Szalai - emptulik at gmail.com on 22.9.2016. */ @Configuration public class HibernateConfiguration implements EnvironmentAware { private DataSource dataSource; private Environment environment; @Override public void setEnvironment(Environment environment) { this.environment = environment; } @Autowired public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public JpaVendorAdapter jpaVendorAdapter() { return new HibernateJpaVendorAdapter(); } @Bean(name = "localContainerEntityManagerFactoryBean") @DependsOn("flyway") public LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); emf.setDataSource(dataSource); emf.setJpaVendorAdapter(jpaVendorAdapter()); emf.setPersistenceUnitName("homiePU"); emf.setPackagesToScan("org.homiefund.api.dao"); emf.setJpaPropertyMap(jpaProperties()); return emf; } private Map<String, Object> jpaProperties() { Map<String, Object> properties = new HashMap<>(); properties.put("hibernate.dialect", environment.getProperty("hibernate.dialect")); properties.put("hibernate.hbm2ddl.auto", environment.getProperty("hibernate.hbm2ddl.auto")); properties.put("hibernate.show_sql", environment.getProperty("hibernate.sql.show", Boolean.class)); properties.put("hibernate.format_sql", environment.getProperty("hibernate.sql.format", Boolean.class)); properties.put("hibernate.jdbc.batch_size", environment.getProperty("hibernate.batch_size", Integer.class)); properties.put("hibernate.default_schema", environment.getProperty("hibernate.default_schema")); properties.put("hibernate.hbm2ddl.charset_name", "UTF-8"); return properties; } @Bean(initMethod = "migrate") public Flyway flyway() { Flyway flyway = new Flyway(); flyway.setBaselineOnMigrate(true); flyway.setLocations("classpath:/flyway/"); flyway.setDataSource(dataSource); return flyway; } }