Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.tamnd.app.config; import java.util.Properties; import javax.persistence.EntityManagerFactory; 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.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * * @author tamnd */ @Configuration @EnableTransactionManagement @ComponentScan(basePackages = { "com.tamnd.app.core.repositories.*" }) public class HibernateConfig { @Bean @Autowired public LocalContainerEntityManagerFactoryBean sessionFactory(DataSource h2DataSource) { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setDataSource(h2DataSource); factory.setPackagesToScan(new String[] { "com.tamnd.app.core.entities" }); factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); factory.setJpaProperties(hibernateProperties()); return factory; } @Bean @Autowired public JpaTransactionManager transactionManager(EntityManagerFactory em) { JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(); jpaTransactionManager.setEntityManagerFactory(em); return jpaTransactionManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } Properties hibernateProperties() { return new Properties() { { setProperty("hibernate.hbm2ddl.auto", "create-drop"); // setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); setProperty("hibernate.format_sql", "true"); setProperty("hibernate.show_sql", "true"); // setProperty("hibernate.globally_quoted_identifiers", "true"); } }; } }