Java tutorial
/* *Copyright 2016 Dominik Szalai (emptulik@gmail.com) * *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. */ /* * Copyright 2016 Dominik Szalai (emptulik@gmail.com) * * 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 cz.muni.fi.editor.database.helpers; import lombok.extern.log4j.Log4j2; 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 8.8.2016. */ @Configuration @Log4j2 public class HibernateConfiguration implements EnvironmentAware { private DataSource dataSource; private Environment env; @Autowired public HibernateConfiguration(DataSource dataSource) { this.dataSource = dataSource; } @Bean public JpaVendorAdapter jpaVendorAdapter() { return new HibernateJpaVendorAdapter(); } @Bean(name = "hibernate") @DependsOn("flyway") public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); emf.setDataSource(dataSource); emf.setJpaVendorAdapter(jpaVendorAdapter()); emf.setPersistenceUnitName("editorPU"); emf.setPackagesToScan("cz.muni.fi.editor.database.domain"); emf.setJpaPropertyMap(jpaProperties()); return emf; } private Map<String, Object> jpaProperties() { Map<String, Object> properties = new HashMap<>(); properties.put("hibernate.dialect", env.getProperty("database.hibernate.dialect")); properties.put("hibernate.hbm2ddl.auto", env.getProperty("database.hibernate.hbm2ddl.auto")); properties.put("hibernate.show_sql", env.getProperty("database.hibernate.sql.show", Boolean.class)); properties.put("hibernate.format_sql", env.getProperty("database.hibernate.sql.format", Boolean.class)); properties.put("hibernate.jdbc.batch_size", env.getProperty("database.hibernate.batch_size", Integer.class)); properties.put("hibernate.default_schema", env.getProperty("database.hibernate.default_schema")); return properties; } @Bean(name = "flyway", initMethod = "migrate") public Flyway flyway() { Flyway flyway = new Flyway(); flyway.setBaselineOnMigrate(true); if (env.acceptsProfiles("!test")) { flyway.setLocations("classpath:/flyway/"); } else { flyway.setLocations("classpath:/flyway-test/"); } flyway.setDataSource(dataSource); return flyway; } @Override public void setEnvironment(Environment environment) { this.env = environment; } }