|
| 1 | +package com.bobocode.config; |
| 2 | + |
| 3 | +import org.springframework.context.annotation.Bean; |
| 4 | +import org.springframework.context.annotation.Configuration; |
| 5 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 6 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 7 | +import org.springframework.orm.jpa.JpaVendorAdapter; |
| 8 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 9 | +import org.springframework.orm.jpa.vendor.Database; |
| 10 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 11 | + |
| 12 | +import javax.sql.DataSource; |
| 13 | + |
| 14 | +@Configuration |
| 15 | +public class JpaConfig { |
| 16 | + @Bean |
| 17 | + DataSource testDataSource() { |
| 18 | + return new EmbeddedDatabaseBuilder() |
| 19 | + .setType(EmbeddedDatabaseType.H2) |
| 20 | + .setName("bobocodeDB") |
| 21 | + .build(); |
| 22 | + } |
| 23 | + |
| 24 | + @Bean |
| 25 | + JpaVendorAdapter testJpaVendorAdapter() { |
| 26 | + HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); |
| 27 | + adapter.setDatabase(Database.H2); |
| 28 | + adapter.setShowSql(true); |
| 29 | + adapter.setGenerateDdl(true); |
| 30 | + adapter.setDatabasePlatform("org.hibernate.dialect.H2Dialect"); |
| 31 | + return adapter; |
| 32 | + } |
| 33 | + |
| 34 | + @Bean("entityManagerFactory") |
| 35 | + LocalContainerEntityManagerFactoryBean localContainerEMF(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { |
| 36 | + LocalContainerEntityManagerFactoryBean lcmfb = new LocalContainerEntityManagerFactoryBean(); |
| 37 | + lcmfb.setDataSource(dataSource); |
| 38 | + lcmfb.setJpaVendorAdapter(jpaVendorAdapter); |
| 39 | + lcmfb.setPersistenceUnitName("bobocode"); |
| 40 | + lcmfb.setPackagesToScan("com.bobocode.model"); |
| 41 | + return lcmfb; |
| 42 | + } |
| 43 | + |
| 44 | + // todo: instead of configuring previous three beans you can use file META-INF/persistence.xml and the following bean |
| 45 | + /*@Bean("entityManagerFactory") |
| 46 | + public LocalEntityManagerFactoryBean entityManagerFactoryBean() { |
| 47 | + LocalEntityManagerFactoryBean emfb = new LocalEntityManagerFactoryBean(); |
| 48 | + emfb.setPersistenceUnitName("bobocode"); |
| 49 | + return emfb; |
| 50 | + }*/ |
| 51 | +} |
0 commit comments