|
| 1 | +package com.iluwatar.repository; |
| 2 | + |
| 3 | +import java.sql.SQLException; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Properties; |
| 6 | + |
| 7 | +import javax.sql.DataSource; |
| 8 | + |
| 9 | +import org.apache.commons.dbcp.BasicDataSource; |
| 10 | +import org.hibernate.jpa.HibernatePersistenceProvider; |
| 11 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 12 | +import org.springframework.context.annotation.Bean; |
| 13 | +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; |
| 14 | +import org.springframework.orm.jpa.JpaTransactionManager; |
| 15 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 16 | + |
| 17 | +/** |
| 18 | + * Annotations based configuration for Spring |
| 19 | + * |
| 20 | + */ |
| 21 | +@EnableJpaRepositories |
| 22 | +public class AppConfig { |
| 23 | + |
| 24 | + /** |
| 25 | + * Creation of H2 db |
| 26 | + * |
| 27 | + * @return A new Instance of DataSource |
| 28 | + */ |
| 29 | + @Bean(destroyMethod = "close") |
| 30 | + public DataSource dataSource() { |
| 31 | + BasicDataSource basicDataSource = new BasicDataSource(); |
| 32 | + basicDataSource.setDriverClassName("org.h2.Driver"); |
| 33 | + basicDataSource.setUrl("jdbc:h2:~/databases/person"); |
| 34 | + basicDataSource.setUsername("sa"); |
| 35 | + basicDataSource.setPassword("sa"); |
| 36 | + return (DataSource) basicDataSource; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Factory to create a especific instance of Entity Manager |
| 41 | + * @return |
| 42 | + */ |
| 43 | + @Bean |
| 44 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
| 45 | + LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); |
| 46 | + entityManager.setDataSource(dataSource()); |
| 47 | + entityManager.setPackagesToScan("com.iluwatar"); |
| 48 | + entityManager.setPersistenceProvider(new HibernatePersistenceProvider()); |
| 49 | + entityManager.setJpaProperties(jpaProperties()); |
| 50 | + |
| 51 | + return entityManager; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Properties for Jpa |
| 56 | + * @return |
| 57 | + */ |
| 58 | + private Properties jpaProperties() { |
| 59 | + Properties properties = new Properties(); |
| 60 | + properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); |
| 61 | + properties.setProperty("hibernate.hbm2ddl.auto", "create-drop"); |
| 62 | + return properties; |
| 63 | + } |
| 64 | + |
| 65 | + @Bean |
| 66 | + public JpaTransactionManager transactionManager() throws SQLException { |
| 67 | + JpaTransactionManager transactionManager = new JpaTransactionManager(); |
| 68 | + transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); |
| 69 | + return transactionManager; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Program entry point |
| 74 | + * |
| 75 | + * @param args command line args |
| 76 | + */ |
| 77 | + public static void main(String[] args) { |
| 78 | + |
| 79 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); |
| 80 | + PersonRepository repository = context.getBean(PersonRepository.class); |
| 81 | + |
| 82 | + Person peter = new Person("Peter", "Sagan", 17); |
| 83 | + Person nasta = new Person("Nasta", "Kuzminova", 25); |
| 84 | + Person john = new Person("John", "lawrence", 35); |
| 85 | + Person terry = new Person("Terry", "Law", 36); |
| 86 | + |
| 87 | + // Add new Person records |
| 88 | + repository.save(peter); |
| 89 | + repository.save(nasta); |
| 90 | + repository.save(john); |
| 91 | + repository.save(terry); |
| 92 | + |
| 93 | + // Count Person records |
| 94 | + System.out.println("Count Person records: " + repository.count()); |
| 95 | + |
| 96 | + // Print all records |
| 97 | + List<Person> persons = (List<Person>) repository.findAll(); |
| 98 | + for (Person person : persons) { |
| 99 | + System.out.println(person); |
| 100 | + } |
| 101 | + |
| 102 | + // Update Person |
| 103 | + nasta.setName("Barbora"); |
| 104 | + nasta.setSurname("Spotakova"); |
| 105 | + repository.save(nasta); |
| 106 | + |
| 107 | + System.out.println("Find by id 2: " + repository.findOne(2L)); |
| 108 | + |
| 109 | + // Remove record from Person |
| 110 | + repository.delete(2L); |
| 111 | + |
| 112 | + // count records |
| 113 | + System.out.println("Count Person records: " + repository.count()); |
| 114 | + |
| 115 | + // find by name |
| 116 | + Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John")); |
| 117 | + System.out.println("Find by John is " + p); |
| 118 | + |
| 119 | + // find by age |
| 120 | + persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40)); |
| 121 | + |
| 122 | + System.out.println("Find Person with age between 20,40: "); |
| 123 | + for (Person person : persons) { |
| 124 | + System.out.println(person); |
| 125 | + } |
| 126 | + |
| 127 | + context.close(); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments