Skip to content

Commit c1b13d2

Browse files
authored
Fix read date from error table which has DynamoDBIndexHashKey when use the tableNamePrefix and make query on the indexed field. (#53)
Co-authored-by: rick.huang <dAAQxwEMb6r7zJIlhOFAV09EtH/ieVlXLmqx3a3daFM=>
1 parent fa78e26 commit c1b13d2

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted;
2727
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter;
2828
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBVersionAttribute;
29+
import org.socialsignin.spring.data.dynamodb.core.DynamoDBOperations;
2930
import org.springframework.util.Assert;
3031
import org.springframework.util.ReflectionUtils;
3132
import org.springframework.util.StringUtils;
@@ -117,6 +118,65 @@ public DynamoDBEntityMetadataSupport(final Class<T> domainType) {
117118
Assert.notNull(hashKeyPropertyName, "Unable to find hash key field or getter method on " + domainType + "!");
118119
}
119120

121+
/**
122+
* Creates a new {@link DynamoDBEntityMetadataSupport} for the given domain
123+
* type and dynamoDB mapper config.
124+
*
125+
* @param domainType
126+
* must not be {@literal null}.
127+
* @param dynamoDBOperations
128+
* must not be {@literal null}.
129+
*/
130+
public DynamoDBEntityMetadataSupport(final Class<T> domainType, DynamoDBOperations dynamoDBOperations) {
131+
132+
Assert.notNull(domainType, "Domain type must not be null!");
133+
this.domainType = domainType;
134+
135+
DynamoDBTable table = this.domainType.getAnnotation(DynamoDBTable.class);
136+
Assert.notNull(table, "Domain type must by annotated with DynamoDBTable!");
137+
138+
this.dynamoDBTableName = dynamoDBOperations.getOverriddenTableName(domainType, table.tableName());
139+
this.hashKeyPropertyName = null;
140+
this.globalSecondaryIndexNames = new HashMap<>();
141+
this.globalIndexHashKeyPropertyNames = new ArrayList<>();
142+
this.globalIndexRangeKeyPropertyNames = new ArrayList<>();
143+
ReflectionUtils.doWithMethods(domainType, method -> {
144+
if (method.getAnnotation(DynamoDBHashKey.class) != null) {
145+
hashKeyPropertyName = getPropertyNameForAccessorMethod(method);
146+
}
147+
if (method.getAnnotation(DynamoDBRangeKey.class) != null) {
148+
hasRangeKey = true;
149+
}
150+
DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = method.getAnnotation(DynamoDBIndexRangeKey.class);
151+
DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = method.getAnnotation(DynamoDBIndexHashKey.class);
152+
153+
if (dynamoDBRangeKeyAnnotation != null) {
154+
addGlobalSecondaryIndexNames(method, dynamoDBRangeKeyAnnotation);
155+
}
156+
if (dynamoDBHashKeyAnnotation != null) {
157+
addGlobalSecondaryIndexNames(method, dynamoDBHashKeyAnnotation);
158+
}
159+
});
160+
ReflectionUtils.doWithFields(domainType, field -> {
161+
if (field.getAnnotation(DynamoDBHashKey.class) != null) {
162+
hashKeyPropertyName = getPropertyNameForField(field);
163+
}
164+
if (field.getAnnotation(DynamoDBRangeKey.class) != null) {
165+
hasRangeKey = true;
166+
}
167+
DynamoDBIndexRangeKey dynamoDBRangeKeyAnnotation = field.getAnnotation(DynamoDBIndexRangeKey.class);
168+
DynamoDBIndexHashKey dynamoDBHashKeyAnnotation = field.getAnnotation(DynamoDBIndexHashKey.class);
169+
170+
if (dynamoDBRangeKeyAnnotation != null) {
171+
addGlobalSecondaryIndexNames(field, dynamoDBRangeKeyAnnotation);
172+
}
173+
if (dynamoDBHashKeyAnnotation != null) {
174+
addGlobalSecondaryIndexNames(field, dynamoDBHashKeyAnnotation);
175+
}
176+
});
177+
Assert.notNull(hashKeyPropertyName, "Unable to find hash key field or getter method on " + domainType + "!");
178+
}
179+
120180
public DynamoDBEntityInformation<T, ID> getEntityInformation() {
121181

122182
if (hasRangeKey) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public DynamoDBRepositoryFactory(DynamoDBOperations dynamoDBOperations) {
100100
@Override
101101
public <T, ID> DynamoDBEntityInformation<T, ID> getEntityInformation(final Class<T> domainClass) {
102102

103-
final DynamoDBEntityMetadataSupport<T, ID> metadata = new DynamoDBEntityMetadataSupport<>(domainClass);
103+
final DynamoDBEntityMetadataSupport<T, ID> metadata = new DynamoDBEntityMetadataSupport<>(domainClass, this.dynamoDBOperations);
104104
return metadata.getEntityInformation();
105105
}
106106

0 commit comments

Comments
 (0)