Java tutorial
/* * Copyright (C) 2012 Mathieu Cambillau <cambillaum@gmail.com> * * 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.cambillaum.jpapersistor.persistence.configuration; import java.util.Properties; import org.apache.commons.dbcp.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.orm.jpa.JpaDialect; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.support.SharedEntityManagerBean; import org.springframework.orm.jpa.vendor.HibernateJpaDialect; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * * @author Mathieu Cambillau <cambillaum@gmail.com> */ @Configuration @EnableTransactionManagement @ComponentScan(basePackages = { "org.cambillaum.jpapersistor" }) public class PersistenceConfiguration { @Bean public BasicDataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); dataSource.setUrl("jdbc:hsqldb:mem:testdb"); dataSource.setUsername("sa"); dataSource.setPassword(""); dataSource.setInitialSize(0); dataSource.setMaxActive(15); dataSource.setMaxIdle(0); dataSource.setMinEvictableIdleTimeMillis(60000); return dataSource; } @Bean public HibernateJpaVendorAdapter jpaVendorAdapter() { HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setShowSql(true); return jpaVendorAdapter; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); entityManagerFactory.setDataSource(dataSource()); Properties jpaProperties = new Properties(); jpaProperties.put("hibernate.show.sql", "true"); jpaProperties.put("hibernate.hbm2ddl.auto", "create-drop"); entityManagerFactory.setJpaProperties(jpaProperties); entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter()); JpaDialect jpaDialect = new HibernateJpaDialect(); entityManagerFactory.setJpaDialect(jpaDialect); entityManagerFactory.setPackagesToScan("org.cambillaum.jpapersistor"); return entityManagerFactory; } @Bean public SharedEntityManagerBean entityManager() { SharedEntityManagerBean entityManager = new SharedEntityManagerBean(); entityManager.setEntityManagerFactory(entityManagerFactory().getObject()); return entityManager; } @Bean public JpaTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setDataSource(dataSource()); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } }