Java tutorial
/* * The MIT License * * Copyright 2013 Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl>. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package net.przemkovv.sphinx.config; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.persistence.EntityManagerFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.orm.hibernate4.HibernateExceptionTranslator; import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.jta.JtaTransactionManager; /** * * @author Przemyslaw Walkowiak <przemyslaw.walkowiak@put.poznan.pl> */ @Configuration @EnableTransactionManagement public class JpaConfig { private static final Logger logger = LoggerFactory.getLogger(JpaConfig.class); // @Bean // public DataSource dataSource() throws NamingException { // logger.debug("Bean initialization: DataSource"); // Context ctx = new InitialContext(); // return (DataSource) ctx.lookup("java:/jboss/datasources/SphinxDS"); // } @Bean public EntityManagerFactory entityManagerFactory() throws NamingException { logger.debug("Bean initialization: EntityManagerFactory"); Context ctx = new InitialContext(); return (EntityManagerFactory) ctx.lookup("java:comp/env/persistence/net.przemkovv.sphinx_PU"); } @Bean public PersistenceAnnotationBeanPostProcessor getPersistenceAnnotationBeanPostProcessor() { logger.debug("Bean initialization: PersistenceAnnotationBeanPostProcessor"); return new PersistenceAnnotationBeanPostProcessor(); } @Bean public PlatformTransactionManager txManager() { return new JtaTransactionManager(); } // // @Bean // public PlatformTransactionManager transactionManager() throws NamingException, Exception { // return new JpaTransactionManager(entityManagerFactory()); // } // // @Bean // public FactoryBean<EntityManagerFactory> entityManagerFactory() throws NamingException { // HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); // vendorAdapter.setDatabase(Database.DERBY); // // vendorAdapter.setGenerateDdl(Boolean.TRUE); // vendorAdapter.setShowSql(Boolean.TRUE); // // LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); // factory.setJpaVendorAdapter(vendorAdapter); // factory.setPackagesToScan("net.przemkovv.sphinx.dao"); // factory.setDataSource(dataSource()); // // Properties jpaProperties = new Properties(); // jpaProperties.setProperty("hibernate.hbm2ddl.auto", "update"); // jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.DerbyDialect"); // jpaProperties.setProperty("hibernate.show_sql", "true"); // jpaProperties.setProperty("hibernate.format_sql", "true"); // factory.setJpaProperties(jpaProperties); // //// factory.afterPropertiesSet(); //// factory.setLoadTimeWeaver(new InstrumentationLoadTimeWeaver()); // //return factory.getObject(); // //Changed as per Josh's suggestion // return factory; // } // // @Bean // public HibernateExceptionTranslator hibernateExceptionTranslator() { // return new HibernateExceptionTranslator(); // } }