Skip to content

Commit f77b4f1

Browse files
committed
Issue #114: Scanning by nested properties
1 parent 917c660 commit f77b4f1

File tree

10 files changed

+294
-9
lines changed

10 files changed

+294
-9
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@
399399
<table>src/test/resources/customerhistory_table.json</table>
400400
<table>src/test/resources/installation_table.json</table>
401401
<table>src/test/resources/auditable_user_table.json</table>
402+
<table>src/test/resources/person_table.json</table>
402403
</tables>
403404
<port>${dynamodblocal.port}</port>
404405
<dist>${project.build.directory}/dynamodb-dist</dist>

src/changes/changes.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 http://maven.apache.org/xsd/changes-1.0.0.xsd">
2121
<properties>
2222
<title>spring-data-dynamodb Changes</title>
23-
<author email="[email protected]" >derjust</author>
23+
<author email="[email protected]">derjust</author>
2424
</properties>
2525
<body>
2626
<release version="5.0.2" date="" description="Maintenance release">

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,21 @@ protected DynamoDBQueryCriteria<T, ID> addCriteria(DynamoDBQueryCriteria<T, ID>
7171
throw new UnsupportedOperationException("Case insensitivity not supported");
7272

7373
Class<?> leafNodePropertyType = part.getProperty().getLeafProperty().getType();
74-
75-
PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
76-
String leafNodePropertyName = leafNodePropertyPath.toDotPath();
74+
75+
part.getProperty().forEach(System.out::println);
76+
77+
//PropertyPath leafNodePropertyPath = part.getProperty().getLeafProperty();
78+
//String leafNodePropertyName = leafNodePropertyPath.toDotPath();
79+
String leafNodePropertyName = part.getProperty().toDotPath();
80+
//TODO #114 - is this correct?
81+
//TODO max deepth of 32 supported by AWS
82+
/*
7783
if (leafNodePropertyName.indexOf(".") != -1)
7884
{
85+
7986
int index = leafNodePropertyName.lastIndexOf(".");
8087
leafNodePropertyName = leafNodePropertyName.substring(index);
81-
}
88+
}*/
8289

8390
switch (part.getType()) {
8491

src/main/java/org/socialsignin/spring/data/dynamodb/repository/support/DynamoDBEntityMetadataSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class DynamoDBEntityMetadataSupport<T, ID> implements DynamoDBHashKeyExtr
5050
private List<String> globalIndexRangeKeyPropertyNames;
5151

5252
private String dynamoDBTableName;
53-
private Map<String, String[]> globalSecondaryIndexNames = new HashMap<String, String[]>();
53+
private Map<String, String[]> globalSecondaryIndexNames = new HashMap<>();
5454

5555
@Override
5656
public String getDynamoDBTableName() {
@@ -70,9 +70,9 @@ public DynamoDBEntityMetadataSupport(final Class<T> domainType) {
7070
DynamoDBTable table = this.domainType.getAnnotation(DynamoDBTable.class);
7171
Assert.notNull(table, "Domain type must by annotated with DynamoDBTable!");
7272
this.dynamoDBTableName = table.tableName();
73-
this.globalSecondaryIndexNames = new HashMap<String, String[]>();
74-
this.globalIndexHashKeyPropertyNames = new ArrayList<String>();
75-
this.globalIndexRangeKeyPropertyNames = new ArrayList<String>();
73+
this.globalSecondaryIndexNames = new HashMap<>();
74+
this.globalIndexHashKeyPropertyNames = new ArrayList<>();
75+
this.globalIndexRangeKeyPropertyNames = new ArrayList<>();
7676
ReflectionUtils.doWithMethods(domainType, new MethodCallback() {
7777
@Override
7878
public void doWith(Method method) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBDocument;
19+
20+
@DynamoDBDocument
21+
public class Address {
22+
private String city;
23+
private String country;
24+
25+
public String getCity() {
26+
return city;
27+
}
28+
public void setCity(String city) {
29+
this.city = city;
30+
}
31+
public String getCountry() {
32+
return country;
33+
}
34+
public void setCountry(String country) {
35+
this.country = country;
36+
}
37+
}
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.repository.config.EnableDynamoDBRepositories;
6+
import org.socialsignin.spring.data.dynamodb.utils.DynamoDBLocalResource;
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 = {DynamoDBLocalResource.class, NestedPropertiesTest.TestAppConfig.class})
18+
public class NestedPropertiesTest {
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,65 @@
1+
/**
2+
* Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
19+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
20+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
21+
import org.springframework.data.annotation.Id;
22+
23+
@DynamoDBTable(tableName = "Person")
24+
public class Person {
25+
26+
@Id
27+
private PersonId personId;
28+
29+
private String phone;
30+
private Address address;
31+
32+
@DynamoDBHashKey
33+
public String getName() {
34+
return personId != null ? personId.getName() : null;
35+
}
36+
37+
@DynamoDBRangeKey
38+
public String getArea() {
39+
return personId != null ? personId.getArea() : null;
40+
}
41+
public Address getAddress() {
42+
return address;
43+
}
44+
public String getPhone() {
45+
return phone;
46+
}
47+
public void setPhone(String phone) {
48+
this.phone = phone;
49+
}
50+
public void setName(String name) {
51+
if (personId == null) {
52+
personId = new PersonId();
53+
}
54+
this.personId.setName(name);
55+
}
56+
public void setArea(String area) {
57+
if (personId == null) {
58+
personId = new PersonId();
59+
}
60+
this.personId.setArea(area);
61+
}
62+
public void setAddress(Address address) {
63+
this.address = address;
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
17+
18+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
19+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
20+
21+
import java.io.Serializable;
22+
23+
public class PersonId implements Serializable {
24+
25+
private static final long serialVersionUID = 1L;
26+
27+
private String name;
28+
private String area;
29+
30+
@DynamoDBRangeKey
31+
public String getArea() {
32+
return area;
33+
}
34+
35+
@DynamoDBHashKey
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public PersonId() {}
41+
42+
public PersonId(String name, String area) {
43+
this.name = name;
44+
this.area = area;
45+
}
46+
public void setName(String name) {
47+
this.name = name;
48+
}
49+
public void setArea(String area) {
50+
this.area = area;
51+
}
52+
}
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright © 2013 spring-data-dynamodb (https://github.com/derjust/spring-data-dynamodb)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.socialsignin.spring.data.dynamodb.repository.query.nested;
17+
18+
import org.socialsignin.spring.data.dynamodb.repository.EnableScan;
19+
import org.springframework.data.repository.PagingAndSortingRepository;
20+
import org.springframework.data.repository.query.Param;
21+
22+
import java.util.List;
23+
24+
public interface PersonRepository extends PagingAndSortingRepository<Person, PersonId> {
25+
26+
Person findByNameAndArea(@Param("name") String name, @Param("area") String area);
27+
28+
@EnableScan
29+
List<Person> findByArea(@Param("area") String area);
30+
31+
@EnableScan
32+
List<Person> findByPhone(@Param("phone") String phone);
33+
34+
@EnableScan
35+
List<Person> findByAddressCountry(@Param("country") String country);
36+
}

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)