|
1 | 1 | package com.bobocode.config; |
2 | 2 |
|
| 3 | +import org.springframework.context.annotation.Bean; |
| 4 | +import org.springframework.context.annotation.Configuration; |
| 5 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
3 | 6 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
4 | 7 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
5 | 8 | import org.springframework.orm.jpa.JpaVendorAdapter; |
6 | 9 | import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 10 | +import org.springframework.orm.jpa.vendor.Database; |
| 11 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
7 | 12 |
|
8 | 13 | import javax.sql.DataSource; |
9 | 14 |
|
|
17 | 22 | * todo: 3. Configure {@link javax.persistence.EntityManagerFactory} bean with name "entityManagerFactory" |
18 | 23 | * todo: 4. Enable JPA repository, set appropriate package using annotation property "basePackages" |
19 | 24 | */ |
| 25 | +@Configuration |
| 26 | +@EnableJpaRepositories(basePackages = "com.bobocode.dao") |
20 | 27 | public class JpaConfig { |
21 | 28 |
|
| 29 | + @Bean |
22 | 30 | public DataSource dataSource() { |
23 | 31 | return new EmbeddedDatabaseBuilder() |
24 | 32 | .setType(EmbeddedDatabaseType.H2) |
25 | 33 | .build(); |
26 | 34 | } |
27 | 35 |
|
| 36 | + @Bean |
28 | 37 | public JpaVendorAdapter jpaVendorAdapter() { |
29 | 38 | // todo: create HibernateJpaVendorAdapter |
| 39 | + HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); |
30 | 40 | // todo: set H2 database |
| 41 | + adapter.setDatabase(Database.H2); |
31 | 42 | // todo: enable DDL generation |
32 | | - throw new UnsupportedOperationException("Application won't start until you provide configs"); |
| 43 | + adapter.setGenerateDdl(true); |
| 44 | + return adapter; |
33 | 45 | } |
34 | 46 |
|
35 | | - public LocalContainerEntityManagerFactoryBean localContainerEMF() { |
| 47 | + @Bean("entityManagerFactory") |
| 48 | + public LocalContainerEntityManagerFactoryBean localContainerEMF(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) { |
36 | 49 | // todo: create and configure required bean |
| 50 | + LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); |
| 51 | + emf.setDataSource(dataSource); |
| 52 | + emf.setJpaVendorAdapter(jpaVendorAdapter); |
37 | 53 | // todo: set package "com.bobocode.model" to scan for JPA entities |
38 | | - throw new UnsupportedOperationException("Application won't start until you provide configs"); |
| 54 | + emf.setPackagesToScan("com.bobocode.model");// JPA entity classes will be loaded from this package |
| 55 | + return emf; |
39 | 56 | } |
40 | 57 | } |
0 commit comments