Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.amanda.curso.config;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.jdbc.datasource.DriverManagerDataSource;
- import org.springframework.orm.jpa.JpaTransactionManager;
- import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
- import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
- import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
- import javax.persistence.EntityManagerFactory;
- import javax.sql.DataSource;
- import java.util.Properties;
- @Configuration
- public class SpringJpaConfig {
- @Bean
- public DataSource dataSource() {
- DriverManagerDataSource ds = new DriverManagerDataSource();
- ds.setDriverClassName("com.mysql.jdbc.Driver");
- ds.setUrl("jdbc:mysql://localhost:3306/test?createDatabaseIfNotExist=true");
- ds.setUsername("root");
- ds.setPassword("root");
- return ds;
- }
- @Bean
- public EntityManagerFactory entityManagerFactory(){
- LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
- factory.setDataSource(dataSource());
- factory.setPackagesToScan("com.amanda.curso.domain");
- factory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
- factory.setJpaProperties(jpaProperties());
- factory.afterPropertiesSet();
- return factory.getObject();
- }
- @Bean
- public JpaTransactionManager transactionManager() {
- JpaTransactionManager tx = new JpaTransactionManager();
- tx.setEntityManagerFactory(entityManagerFactory());
- tx.setJpaDialect(new HibernateJpaDialect());
- return tx;
- }
- private Properties jpaProperties() {
- Properties props = new Properties();
- props.setProperty("hibernate.show_sql", "true");
- props.setProperty("hibernate.format_sql", "true");
- props.setProperty("hibernate.hbm2ddl.auto", "update");
- return props;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement