Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.myapp.spring.config;
- import java.util.Calendar;
- import java.util.Date;
- 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.Conditional;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import org.springframework.scheduling.TaskScheduler;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
- import org.springframework.transaction.PlatformTransactionManager;
- @Configuration()
- @ComponentScan(basePackages="com.myapp.spring")
- public class AppConfig {
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource dataSource=new DriverManagerDataSource();
- dataSource.setDriverClassName("com.mysql.jdbc.Driver");
- dataSource.setUrl("jdbc:mysql://localhost:3306/hibernatespring");
- dataSource.setUsername("admin");
- dataSource.setPassword("admin");
- return dataSource;
- }
- @Bean
- public Properties jpaProperties() {
- Properties properties=new Properties();
- properties.put("hibernate.hbm2ddl.auto", "update");
- properties.put("hibernate.show_sql", "true");
- return properties;
- }
- // Spring Provided Class and implements EntityManagerFactory of JPA
- @Bean(name="entityManagerFactory")
- @Autowired
- public LocalContainerEntityManagerFactoryBean entityManagerFactory(
- DataSource dataSource,Properties jpaProperties) {
- LocalContainerEntityManagerFactoryBean emf=
- new LocalContainerEntityManagerFactoryBean();
- emf.setDataSource(dataSource);
- emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
- emf.setPackagesToScan("com.myapp.spring.hibernate.model");
- emf.setJpaProperties(jpaProperties);
- emf.setPersistenceUnitName("default");
- emf.afterPropertiesSet();
- return emf;
- }
- @Bean
- @Autowired
- public PlatformTransactionManager transactionManager(
- EntityManagerFactory entityManagerFactory) {
- return new JpaTransactionManager(entityManagerFactory);
- }
- }
Add Comment
Please, Sign In to add comment