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