Java tutorial
/** * Copyright (C) - 2013 jbrule * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later * version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/> */ package org.jbr.taskmgr.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Profile; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; 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; /** * Configures the database components for the Task Manager application. * * @author <a href="mailto:brulejr@gmail.com">Jon Brule</a> */ @Configuration @EnableJpaRepositories(basePackages = "org.jbr.taskmgr.repository") @EnableTransactionManagement public class TaskManagerDatabaseConfig { @Configuration @Profile({ "LOCAL" }) public static class EmbeddedDatabase { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL) .addScript("classpath:database/taskmgr-ddl.sql").addScript("classpath:database/taskmgr-dml.sql") .build(); } @Bean public HibernateJpaVendorAdapter jpaVendorAdapter() { final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setDatabase(Database.HSQL); vendorAdapter.setGenerateDdl(false); vendorAdapter.setShowSql(true); return vendorAdapter; } } @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(final DataSource dataSource, final JpaVendorAdapter jpaVendorAdapter) { final LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(jpaVendorAdapter); factory.setPackagesToScan("org.jbr.taskmgr.model.domain"); factory.setDataSource(dataSource); return factory; } @Bean @Primary public PlatformTransactionManager transactionManager( final LocalContainerEntityManagerFactoryBean entityManagerFactory) { final JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory.getObject()); return txManager; } }