Java tutorial
/** * Copyright (c) 2016 All Rights Reserved by the SDL Group. * * 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 com.sdl.odata.datasource.jpa; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import javax.sql.DataSource; /** * The JPA Datasource configuration. * @author Renze de Vries */ @Configuration @ComponentScan public class JPADataSourceConfiguration { private static final Logger LOG = LoggerFactory.getLogger(JPADataSourceConfiguration.class); @Value("${datasource.driver}") private String driver; @Value("${datasource.username}") private String username; @Value("${datasource.password}") private String password; @Value("${datasource.url}") private String url; @Value("${datasource.dialect}") private String dialect; @Value("${datasource.entitymodel}") private String entityModel; @Value("${datasource.generateDDL:true}") private boolean generateDDL; @Bean @Primary @ConfigurationProperties(prefix = "datasource.primary") public DataSource primaryDataSource() { LOG.info("Logging in with user {} and url {}", username, url); return DataSourceBuilder.create().url(url).driverClassName(driver).username(username).password(password) .build(); } @Bean public JpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setDatabasePlatform(dialect); jpaVendorAdapter.setGenerateDdl(generateDDL); return jpaVendorAdapter; } @Bean @Primary public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); localContainerEntityManagerFactoryBean.setDataSource(primaryDataSource()); localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter()); localContainerEntityManagerFactoryBean.setPackagesToScan(entityModel); localContainerEntityManagerFactoryBean.afterPropertiesSet(); return localContainerEntityManagerFactoryBean; } }