config.DomainAndPersistenceConfig.java Source code

Java tutorial

Introduction

Here is the source code for config.DomainAndPersistenceConfig.java

Source

/*
 * 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 config;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 *
 * @author Soukaina
 */

@EnableJpaRepositories(basePackages = { "cabinet.repositories", "cabinet.security" })
@EnableAutoConfiguration
@ComponentScan(basePackages = { "cabinet" })
@EntityScan(basePackages = { "cabinet.entities", "cabinet.security" })
@EnableTransactionManagement
public class DomainAndPersistenceConfig {

    // la source de donnes MySQL
    @Bean
    public DataSource dataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/cabinet");
        dataSource.setUsername("root");
        dataSource.setPassword("");
        return dataSource;
    }

    // le provider JPA - n'est pas ncessaire si on est satisfait des valeurs par
    // dfaut utilises par Spring boot
    // ici on le dfinit pour activer / dsactiver les logs SQL
    @Bean
    public JpaVendorAdapter jpaVendorAdapter() {
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setShowSql(false);
        hibernateJpaVendorAdapter.setGenerateDdl(false);
        hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
        return hibernateJpaVendorAdapter;
    }

    // l'EntityManageFactory et le TransactionManager sont dfinis avec des
    // valeurs par dfaut par Spring boot

}