/* * Copyright 2012 the original author or authors. * * 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.github.carlomicieli.config; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * The configuration class. * @author Carlo Micieli * */ @Configuration @EnableTransactionManagement @PropertySource("classpath:META-INF/config/app.properties") @ImportResource("classpath:META-INF/spring.xml") @ComponentScan(basePackages = "com.github.carlomicieli") public class ApplicationConfig { private @Autowired Environment env; public @Bean LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() { LocalContainerEntityManagerFactoryBean emFactory = new LocalContainerEntityManagerFactoryBean(); emFactory.setDataSource(dataSource()); emFactory.setPackagesToScan("com.github.carlomicieli.entities"); HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); vendorAdapter.setShowSql(true); vendorAdapter.setDatabasePlatform(env.getProperty("dialect")); emFactory.setJpaVendorAdapter(vendorAdapter); return emFactory; } /** * Return the data source bean. * @return the data source bean. */ public @Bean DataSource dataSource() { ComboPooledDataSource ds = new ComboPooledDataSource(); try { ds.setDriverClass(env.getProperty("connection.driver_class")); } catch (Exception e) { throw new RuntimeException(e); } ds.setJdbcUrl(env.getProperty("connection.url")); ds.setUser(env.getProperty("connection.username")); ds.setPassword(env.getProperty("connection.password")); return ds; } public @Bean PlatformTransactionManager transactionManager(){ JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory( entityManagerFactoryBean().getObject() ); return transactionManager; } }