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 co.id.ipb.ilkom.training.db; import java.util.Properties; import javax.persistence.EntityManagerFactory; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 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 ifnu.b.fatkhan */ @Configuration @EnableJpaRepositories(basePackages = { "co.id.ipb.ilkom" }) @ComponentScan("co.id.ipb.ilkom") @EnableTransactionManagement public class SpringDataJpaConfiguration { @Bean DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl("jdbc:mysql://localhost:3306/trainingjava"); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername("root"); dataSource.setPassword("root"); dataSource.setInitialSize(20); dataSource.setMaxIdle(20); dataSource.setMaxTotal(100); return dataSource; } @Bean LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) { LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean(); entityManagerFactoryBean.setDataSource(dataSource); entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); entityManagerFactoryBean.setPackagesToScan("co.id.ipb.ilkom"); Properties jpaProperties = new Properties(); //Configures the used database dialect. This allows Hibernate to create SQL //that is optimized for the used database. jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); //Specifies the action that is invoked to the database when the Hibernate //SessionFactory is created or closed. jpaProperties.put("hibernate.hbm2ddl.auto", "update"); //If the value of this property is true, Hibernate writes all SQL //statements to the console. jpaProperties.put("hibernate.show_sql", "true"); //If the value of this property is true, Hibernate will format the SQL //that is written to the console. jpaProperties.put("hibernate.format_sql", "true"); entityManagerFactoryBean.setJpaProperties(jpaProperties); return entityManagerFactoryBean; } @Bean JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory); return transactionManager; } }