Skip to content

Commit a22eb79

Browse files
committed
Issue #114: Scanning by nested properties
1 parent c3eab0d commit a22eb79

File tree

8 files changed

+223
-1
lines changed

8 files changed

+223
-1
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@
339339
<table>src/test/resources/customerhistory_table.json</table>
340340
<table>src/test/resources/installation_table.json</table>
341341
<table>src/test/resources/auditable_user_table.json</table>
342+
<table>src/test/resources/person_table.json</table>
342343
</tables>
343344
<port>${dynamodblocal.port}</port>
344345
<dist>${project.build.directory}/dynamodb-dist</dist>

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/AbstractDynamoDBQueryCreator.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,15 @@ protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID>
7474

7575
PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
7676
String leafNodePropertyName = leafNodePropertyPath.toDotPath();
77+
//TODO #114 - is this correct?
78+
//TODO max deepth of 32 supported by AWS
79+
/*
7780
if (leafNodePropertyName.indexOf(".") != -1)
7881
{
82+
7983
int index = leafNodePropertyName.lastIndexOf(".");
8084
leafNodePropertyName = leafNodePropertyName.substring(index);
81-
}
85+
}*/
8286

8387
switch (part.getType()) {
8488

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
4+
5+
@DynamoDBDocument
6+
public class Address {
7+
private String city;
8+
private String country;
9+
10+
public String getCity() {
11+
return city;
12+
}
13+
public void setCity(String city) {
14+
this.city = city;
15+
}
16+
public String getCountry() {
17+
return country;
18+
}
19+
public void setCountry(String country) {
20+
this.country = country;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.socialsignin.spring.data.dynamodb.core.ConfigurationTI;
6+
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.test.context.ContextConfiguration;
10+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11+
12+
import java.util.List;
13+
14+
import static org.junit.Assert.assertEquals;
15+
16+
@RunWith(SpringJUnit4ClassRunner.class)
17+
@ContextConfiguration(classes = {ConfigurationTI.class, NestedPropertiesIT.TestAppConfig.class})
18+
public class NestedPropertiesIT {
19+
20+
@Configuration
21+
@EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.query.nested")
22+
public static class TestAppConfig {
23+
}
24+
25+
@Autowired
26+
private PersonRepository personRepository;
27+
28+
@Test
29+
public void testNestedProperty() {
30+
31+
Address usaAddress = new Address();
32+
usaAddress.setCity("New York");
33+
usaAddress.setCountry("USA");
34+
35+
Address deAddress = new Address();
36+
deAddress.setCity("Frankfurt");
37+
deAddress.setCity("Germany");
38+
39+
Person p1 = new Person();
40+
p1.setName("personName");
41+
p1.setPhone("phone");
42+
p1.setArea("area");
43+
p1.setAddress(usaAddress);
44+
45+
Person p2 = new Person();
46+
p2.setName("otherName");
47+
p2.setPhone("42");
48+
p2.setArea("otherArea");
49+
p2.setAddress(deAddress);
50+
51+
/// personRepository.save(p1);
52+
53+
List<Person> actual = personRepository.findByAddressCountry("USA");
54+
assertEquals(1, actual.size());
55+
56+
actual = personRepository.findByPhone("42");
57+
assertEquals(1, actual.size());
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
5+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
6+
import org.springframework.data.annotation.Id;
7+
8+
@DynamoDBTable(tableName = "Person")
9+
public class Person {
10+
11+
@Id
12+
private PersonId personId;
13+
14+
private String phone;
15+
private Address address;
16+
17+
@DynamoDBHashKey
18+
public String getName() {
19+
return personId != null ? personId.getName() : null;
20+
}
21+
22+
@DynamoDBRangeKey
23+
public String getArea() {
24+
return personId != null ? personId.getArea() : null;
25+
}
26+
public Address getAddress() {
27+
return address;
28+
}
29+
public String getPhone() {
30+
return phone;
31+
}
32+
public void setPhone(String phone) {
33+
this.phone = phone;
34+
}
35+
public void setName(String name) {
36+
if (personId == null) {
37+
personId = new PersonId();
38+
}
39+
this.personId.setName(name);
40+
}
41+
public void setArea(String area) {
42+
if (personId == null) {
43+
personId = new PersonId();
44+
}
45+
this.personId.setArea(area);
46+
}
47+
public void setAddress(Address address) {
48+
this.address = address;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
5+
6+
import java.io.Serializable;
7+
8+
public class PersonId implements Serializable {
9+
10+
private static final long serialVersionUID = 1L;
11+
12+
private String name;
13+
private String area;
14+
15+
@DynamoDBRangeKey
16+
public String getArea() {
17+
return area;
18+
}
19+
20+
@DynamoDBHashKey
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public PersonId() {}
26+
27+
public PersonId(String name, String area) {
28+
this.name = name;
29+
this.area = area;
30+
}
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
public void setArea(String area) {
35+
this.area = area;
36+
}
37+
}
38+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
2+
3+
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
4+
import org.springframework.data.repository.PagingAndSortingRepository;
5+
import org.springframework.data.repository.query.Param;
6+
7+
import java.util.List;
8+
9+
public interface PersonRepository extends PagingAndSortingRepository<Person, PersonId> {
10+
11+
Person findByNameAndArea(@Param("name") String name, @Param("area") String area);
12+
13+
@EnableScan
14+
List<Person> findByArea(@Param("area") String area);
15+
16+
@EnableScan
17+
List<Person> findByPhone(@Param("phone") String phone);
18+
19+
@EnableScan
20+
List<Person> findByAddressCountry(@Param("country") String country);
21+
}

src/test/resources/person_table.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"AttributeDefinitions": [
3+
{
4+
"AttributeName": "Name",
5+
"AttributeType": "S"
6+
},
7+
{
8+
"AttributeName": "Area",
9+
"AttributeType": "S"
10+
}
11+
],
12+
"KeySchema": [
13+
{
14+
"AttributeName": "Name",
15+
"KeyType": "HASH"
16+
},
17+
{
18+
"AttributeName": "Area",
19+
"KeyType": "RANGE"
20+
}
21+
],
22+
"ProvisionedThroughput": {
23+
"ReadCapacityUnits": "10",
24+
"WriteCapacityUnits": "10"
25+
},
26+
"TableName": "person"
27+
}

0 commit comments

Comments
 (0)