@@ -82,37 +82,27 @@ Create a DynamoDB entity for this table:
8282@DynamoDBTable (tableName = " User" )
8383public class User {
8484
85- private String id;
86- private String firstName;
87- private String lastName;
88-
89- @DynamoDBHashKey
90- @DynamoDBAutoGeneratedKey
91- public String getId () {
92- return id;
93- }
94-
95- @DynamoDBAttribute
96- public String getFirstName () {
97- return firstName;
98- }
99-
100- @DynamoDBAttribute
101- public String getLastName () {
102- return lastName;
103- }
104-
105- public void setId (String id ) {
106- this . id = id;
107- }
108-
109- public void setFirstName (String firstName ) {
110- this . firstName = firstName;
111- }
85+ @DynamoDBHashKey
86+ @DynamoDBAutoGeneratedKey
87+ private String id;
88+
89+ @DynamoDBAttribute
90+ private String firstName;
91+
92+ @DynamoDBAttribute
93+ private String lastName;
94+
95+ public User (String firstName , String lastName ) {
96+ this . firstName = firstName;
97+ this . lastName = lastName;
98+ }
99+
100+ public User () {
101+ super ();
102+ }
103+
104+ // Getters and setters
112105
113- public void setLastName (String lastName ) {
114- this . lastName = lastName;
115- }
116106}
117107```
118108
@@ -123,7 +113,9 @@ package com.acme.repositories;
123113
124114@EnableScan
125115public interface UserRepository extends CrudRepository<User , String > {
126- List<User > findByLastName (String lastName );
116+
117+ List<User > findByLastName (String lastName );
118+ List<User > findByFirstName (String firstName );
127119}
128120```
129121
@@ -133,7 +125,9 @@ or for paging and sorting...
133125package com.acme.repositories ;
134126
135127public interface UserRepository extends PagingAndSortingRepository<User , String > {
136- Page<User > findByLastName (String lastName ,Pageable pageable );
128+
129+ Page<User > findByLastName (String lastName , Pageable pageable );
130+ Page<User > findByFirstName (String firstName , Pageable pageable );
137131
138132 @EnableScan
139133 @EnableScanCount
@@ -145,45 +139,40 @@ And finally write a test client
145139
146140``` java
147141@RunWith (SpringJUnit4ClassRunner . class)
148- @SpringApplicationConfiguration (classes = {
149- PropertyPlaceholderAutoConfiguration . class, DynamoDBConfig . class})
150- public class UserRepositoryIntegrationTest {
151-
142+ @SpringBootTest
143+ public class UserRepositoryIntegrationTest {
144+
152145 @Autowired
153- UserRepository repository;
154-
146+ private UserRepository repository;
147+
155148 @Test
156149 public void sampleTestCase () {
157150 User dave = new User (" Dave" , " Matthews" );
158151 repository. save(dave);
159-
152+
160153 User carter = new User (" Carter" , " Beauford" );
161154 repository. save(carter);
162-
163- List<User > result = repository. findByLastName(" Matthews" );
164- Assert . assertThat(result. size(), is(1 ));
165- Assert . assertThat(result, hasItem(dave));
155+
156+ List<User > matthewsFound = repository. findByLastName(" Matthews" );
157+ Assert . assertFalse(matthewsFound. isEmpty());
158+
159+ List<User > cartersFound = repository. findByFirstName(" Carter" );
160+ Assert . assertFalse(cartersFound. isEmpty());
161+
166162 }
167-
163+
168164 private static final long CAPACITY = 5L ;
169-
165+
170166 @Autowired
171167 private AmazonDynamoDB amazonDynamoDB;
172-
168+
173169 @Before
174170 public void init () throws Exception {
175- // Delete User table in case it exists
176- amazonDynamoDB. listTables(). getTableNames(). stream().
177- filter(tableName - > tableName. equals(User . TABLE_NAME )). forEach(tableName - > {
178- amazonDynamoDB. deleteTable(tableName);
179- });
180-
181- // Create User table
182- amazonDynamoDB. createTable(new DynamoDBMapper (amazonDynamoDB)
183- .generateCreateTableRequest(User . class)
184- .withProvisionedThroughput(new ProvisionedThroughput (CAPACITY , CAPACITY )));
171+ TableUtils . createTableIfNotExists(amazonDynamoDB, new DynamoDBMapper (amazonDynamoDB)
172+ .generateCreateTableRequest(User . class)
173+ .withProvisionedThroughput(new ProvisionedThroughput (CAPACITY , CAPACITY )));
185174 }
186-
175+
187176}
188177```
189178
0 commit comments