package com.artandscience.security.configuration; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import jakarta.persistence.EntityManagerFactory; import java.util.Properties; @Configuration @Profile("test") @EnableTransactionManagement public class H2TestProfileJPAConfig { @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("org.h2.Driver"); // in-memory H2 dataSource.setUrl( "jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;MODE=PostgreSQL;" + "INIT=CREATE SCHEMA IF NOT EXISTS SECURITY\\;SET SCHEMA SECURITY" ); dataSource.setUsername("sa"); dataSource.setPassword("sa"); return dataSource; } @Bean(name = "entityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( @Qualifier("dataSource") DataSource dataSource ) { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); // *** KENDİ ENTITY PAKETİNİ YAZ *** em.setPackagesToScan("com.artandscience.security.dao.entity"); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); em.setJpaProperties(hibernateProperties()); return em; } @Bean public PlatformTransactionManager transactionManager( EntityManagerFactory entityManagerFactory ) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory); return txManager; } @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } private Properties hibernateProperties() { Properties properties = new Properties(); properties.put("hibernate.hbm2ddl.auto", "create-drop"); // testte tabloyu otomatik oluştur/sil properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); properties.put("hibernate.show_sql", "true"); properties.put("hibernate.format_sql", "true"); properties.put("hibernate.default_schema", "SECURITY"); // Eğer SECURITY gibi bir şema kullanıyorsan: // properties.put("hibernate.default_schema", "SECURITY"); return properties; } }