Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 619c2b9

Browse files
committed
Complete the task "Hello JPA Repository"
1 parent 2491af0 commit 619c2b9

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

hello-jpa-repository/src/main/java/com/bobocode/config/JpaConfig.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.bobocode.config;
22

3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
36
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
47
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
58
import org.springframework.orm.jpa.JpaVendorAdapter;
69
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
10+
import org.springframework.orm.jpa.vendor.Database;
11+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
712

813
import javax.sql.DataSource;
914

@@ -17,24 +22,36 @@
1722
* todo: 3. Configure {@link javax.persistence.EntityManagerFactory} bean with name "entityManagerFactory"
1823
* todo: 4. Enable JPA repository, set appropriate package using annotation property "basePackages"
1924
*/
25+
@Configuration
26+
@EnableJpaRepositories(basePackages = "com.bobocode.dao")
2027
public class JpaConfig {
2128

29+
@Bean
2230
public DataSource dataSource() {
2331
return new EmbeddedDatabaseBuilder()
2432
.setType(EmbeddedDatabaseType.H2)
2533
.build();
2634
}
2735

36+
@Bean
2837
public JpaVendorAdapter jpaVendorAdapter() {
2938
// todo: create HibernateJpaVendorAdapter
39+
HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
3040
// todo: set H2 database
41+
adapter.setDatabase(Database.H2);
3142
// todo: enable DDL generation
32-
throw new UnsupportedOperationException("Application won't start until you provide configs");
43+
adapter.setGenerateDdl(true);
44+
return adapter;
3345
}
3446

35-
public LocalContainerEntityManagerFactoryBean localContainerEMF() {
47+
@Bean("entityManagerFactory")
48+
public LocalContainerEntityManagerFactoryBean localContainerEMF(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
3649
// todo: create and configure required bean
50+
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
51+
emf.setDataSource(dataSource);
52+
emf.setJpaVendorAdapter(jpaVendorAdapter);
3753
// 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;
3956
}
4057
}

hello-jpa-repository/src/main/java/com/bobocode/config/RootConfig.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package com.bobocode.config;
22

3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.orm.jpa.JpaTransactionManager;
37
import org.springframework.transaction.PlatformTransactionManager;
8+
import org.springframework.transaction.annotation.EnableTransactionManagement;
9+
10+
import javax.persistence.EntityManagerFactory;
411

512
/**
613
* This class is provides root Java config for Spring application.
@@ -10,6 +17,14 @@
1017
* todo: 2. Configure {@link PlatformTransactionManager} bean with name "transactionManager"
1118
* todo: 3. Enable transaction management
1219
*/
20+
@Configuration
21+
@ComponentScan("com.bobocode")
22+
@EnableTransactionManagement
1323
public class RootConfig {
24+
25+
@Bean
26+
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
27+
return new JpaTransactionManager(entityManagerFactory);
28+
}
1429
}
1530

hello-jpa-repository/src/main/java/com/bobocode/dao/UserRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
* <p>
99
* todo: 1. Configure {@link UserRepository} as {@link JpaRepository} for class User
1010
*/
11-
public interface UserRepository extends JpaRepository {
11+
public interface UserRepository extends JpaRepository<User, Long> {
1212

1313
}

0 commit comments

Comments
 (0)