java – spring boot hibernate configuration

java – spring boot hibernate configuration

Add @EnableJPARepositories to your Application

try commenting out the @Transactional annotation

   //@Transactional
   public interface PaymentDao extends CrudRepository<Payment, Long> {

    }

java – spring boot hibernate configuration

This is the application.property file infomation

    spring.mysql.datasource.url=jdbc:mysql://localhost:3306/testDB
    spring.mysql.datasource.username=test
    spring.mysql.datasource.password=test
    spring.mysql.datasource.driver-class-name=com.mysql.jdbc.Driver

This is the java based configuration

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = mysqlEntityManager,
        transactionManagerRef = mysqlTransactionManager,
        basePackages = lk.test.dao
)
@PropertySource(classpath:application.properties)
public class MySqlDBConfig {

    @Autowired
    Environment environment;


    /**
     * These cant be  Primary
     *
     * @return
     */

    @Bean(name = mysqlDatasource)
    @ConfigurationProperties(prefix = spring.mysql.datasource)
    public DataSource mySqlDataSource() {
       DriverManagerDataSource dataSource = new DriverManagerDataSource();               
        dataSource.setDriverClassName(environment.getProperty(spring.mysql.datasource.driver-class-name));
        dataSource.setUrl(environment.getProperty(spring.mysql.datasource.url));
        dataSource.setUsername(environment.getProperty(spring.mysql.datasource.username));
        dataSource.setPassword(environment.getProperty(spring.mysql.datasource.password));
        return dataSource;
    }


    @Bean(name = mysqlEntityManager)
    public LocalContainerEntityManagerFactoryBean mysqlEntityManagerFactoryBean(@Qualifier(mysqlDatasource) DataSource dataSource) {
        LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
        emf.setDataSource(dataSource);
        emf.setPersistenceUnitName(mysqlPU);
        emf.setJpaProperties(hibernateProperties());
        emf.setPackagesToScan(lk.test.dto.mysql); 
        emf.setJpaVendorAdapter(getHibernateAdapter());
        return emf;
    }

    @Bean(name = mysqlTransactionManager)
    public PlatformTransactionManager mySqlTransactionManager(@Qualifier(mysqlEntityManager) EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

    private Properties hibernateProperties() {
        Properties props = new Properties();
        props.setProperty(hibernate.dialect, org.hibernate.dialect.MySQLDialect);
        props.setProperty(hibernate.show_sql, true);
        return props;
    }

    @Bean
    public JpaVendorAdapter getHibernateAdapter() {
        return new HibernateJpaVendorAdapter();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *