|
| 1 | +package com.baeldung.manytomany; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import javax.sql.DataSource; |
| 7 | + |
| 8 | +import org.springframework.beans.factory.annotation.Value; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.Configuration; |
| 11 | +import org.springframework.context.annotation.PropertySource; |
| 12 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 13 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 14 | +import org.springframework.orm.jpa.JpaVendorAdapter; |
| 15 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 16 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 17 | + |
| 18 | +@Configuration |
| 19 | +@PropertySource("classpath:/manytomany/test.properties") |
| 20 | +public class ManyToManyTestConfiguration { |
| 21 | + |
| 22 | + @Bean |
| 23 | + public DataSource dataSource() { |
| 24 | + EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder(); |
| 25 | + return dbBuilder.setType(EmbeddedDatabaseType.H2) |
| 26 | + .addScript("classpath:/manytomany/db.sql") |
| 27 | + .build(); |
| 28 | + } |
| 29 | + |
| 30 | + @Bean |
| 31 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${hibernate.hbm2ddl.auto}") String hbm2ddlType, @Value("${hibernate.dialect}") String dialect, @Value("${hibernate.show_sql}") boolean showSql) { |
| 32 | + LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean(); |
| 33 | + |
| 34 | + result.setDataSource(dataSource()); |
| 35 | + result.setPackagesToScan("com.baeldung.manytomany.model"); |
| 36 | + result.setJpaVendorAdapter(jpaVendorAdapter()); |
| 37 | + |
| 38 | + Map<String, Object> jpaProperties = new HashMap<>(); |
| 39 | + jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddlType); |
| 40 | + jpaProperties.put("hibernate.dialect", dialect); |
| 41 | + jpaProperties.put("hibernate.show_sql", showSql); |
| 42 | + result.setJpaPropertyMap(jpaProperties); |
| 43 | + |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + public JpaVendorAdapter jpaVendorAdapter() { |
| 48 | + return new HibernateJpaVendorAdapter(); |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments