-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Allow Long as ID type in Entity for Cosmos DB #13321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
1949d5b
70aa09c
1e42ed2
103ffa2
2aa5b64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,13 +166,12 @@ public <T> T findById(Object id, Class<T> domainType) { | |
| public <T> T findById(Object id, Class<T> domainType, PartitionKey partitionKey) { | ||
| Assert.notNull(domainType, "domainType should not be null"); | ||
| Assert.notNull(partitionKey, "partitionKey should not be null"); | ||
| assertValidId(id); | ||
|
|
||
| String idToQuery = (String) CosmosUtils.getStringIDValue(id); | ||
| final String containerName = getContainerName(domainType); | ||
| return cosmosAsyncClient | ||
| .getDatabase(databaseName) | ||
| .getContainer(containerName) | ||
| .readItem(id.toString(), partitionKey, JsonNode.class) | ||
| .readItem(idToQuery, partitionKey, JsonNode.class) | ||
| .flatMap(cosmosItemResponse -> { | ||
| CosmosUtils.fillAndProcessResponseDiagnostics(responseDiagnosticsProcessor, | ||
| cosmosItemResponse.getDiagnostics(), null); | ||
|
|
@@ -195,10 +194,9 @@ public <T> T findById(Object id, Class<T> domainType, PartitionKey partitionKey) | |
| public <T> T findById(String containerName, Object id, Class<T> domainType) { | ||
| Assert.hasText(containerName, "containerName should not be null, empty or only whitespaces"); | ||
| Assert.notNull(domainType, "domainType should not be null"); | ||
| assertValidId(id); | ||
|
|
||
| final String query = String.format("select * from root where root.id = '%s'", | ||
| id.toString()); | ||
| CosmosUtils.getStringIDValue(id)); | ||
| final CosmosQueryRequestOptions options = new CosmosQueryRequestOptions(); | ||
| options.setQueryMetricsEnabled(enableQueryMetrics); | ||
| return cosmosAsyncClient | ||
|
|
@@ -429,8 +427,7 @@ public CosmosContainerProperties createContainerIfNotExists(CosmosEntityInformat | |
| */ | ||
| public void deleteById(String containerName, Object id, PartitionKey partitionKey) { | ||
| Assert.hasText(containerName, "containerName should not be null, empty or only whitespaces"); | ||
| assertValidId(id); | ||
|
|
||
| String idToDelete = (String) CosmosUtils.getStringIDValue(id); | ||
| LOGGER.debug("execute deleteById in database {} container {}", this.databaseName, | ||
| containerName); | ||
|
|
||
|
|
@@ -439,7 +436,7 @@ public void deleteById(String containerName, Object id, PartitionKey partitionKe | |
| } | ||
| cosmosAsyncClient.getDatabase(this.databaseName) | ||
| .getContainer(containerName) | ||
| .deleteItem(id.toString(), partitionKey) | ||
| .deleteItem(idToDelete, partitionKey) | ||
| .doOnNext(response -> | ||
| CosmosUtils.fillAndProcessResponseDiagnostics(responseDiagnosticsProcessor, | ||
| response.getDiagnostics(), null)) | ||
|
|
@@ -454,9 +451,12 @@ public <T, ID> List<T> findByIds(Iterable<ID> ids, Class<T> domainType, String c | |
| Assert.notNull(ids, "Id list should not be null"); | ||
| Assert.notNull(domainType, "domainType should not be null."); | ||
| Assert.hasText(containerName, "container should not be null, empty or only whitespaces"); | ||
|
|
||
| final List<Object> idList = new ArrayList<>(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need this change for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be, but there's o similar method in ReactiveCosmosTemplate, I unified the procesing of ID type conversion.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good, makes sense. |
||
| for (ID id : ids) { | ||
| idList.add(CosmosUtils.getStringIDValue(id)); | ||
| } | ||
| final DocumentQuery query = new DocumentQuery(Criteria.getInstance(CriteriaType.IN, "id", | ||
| Collections.singletonList(ids), Part.IgnoreCaseType.NEVER)); | ||
| Collections.singletonList(idList), Part.IgnoreCaseType.NEVER)); | ||
| return find(query, domainType, containerName); | ||
| } | ||
|
|
||
|
|
@@ -670,13 +670,6 @@ private List<String> getPartitionKeyNames(Class<?> domainType) { | |
| return Collections.singletonList(entityInfo.getPartitionKeyFieldName()); | ||
| } | ||
|
|
||
| private void assertValidId(Object id) { | ||
| Assert.notNull(id, "id should not be null"); | ||
| if (id instanceof String) { | ||
| Assert.hasText(id.toString(), "id should not be empty or only whitespaces."); | ||
| } | ||
| } | ||
|
|
||
| private <T> List<JsonNode> findItems(@NonNull DocumentQuery query, | ||
| @NonNull String containerName) { | ||
| final SqlQuerySpec sqlQuerySpec = new FindQuerySpecGenerator().generateCosmos(query); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -201,8 +201,10 @@ private Field getIdField(Class<?> domainType) { | |
| throw new IllegalArgumentException("domain should contain @Id field or field named id"); | ||
| } else if (idField.getType() != String.class | ||
| && idField.getType() != Integer.class | ||
| && idField.getType() != int.class) { | ||
| throw new IllegalArgumentException("type of id field must be String or Integer"); | ||
| && idField.getType() != int.class | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a unit test for this in
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in CosmosEntityInformationUnitTest, please check
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. |
||
| && idField.getType() != Long.class | ||
| && idField.getType() != long.class) { | ||
| throw new IllegalArgumentException("type of id field must be String, Integer or Long"); | ||
| } | ||
|
|
||
| return idField; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| package com.azure.spring.data.cosmos.domain; | ||
|
|
||
| import com.azure.spring.data.cosmos.core.mapping.Document; | ||
| import org.springframework.data.annotation.Id; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @Document | ||
| public class LongIdDomain { | ||
|
|
||
| @Id | ||
| private Long number; | ||
|
|
||
| private String name; | ||
|
|
||
| public LongIdDomain(Long number, String name) { | ||
| this.number = number; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public LongIdDomain() { | ||
| } | ||
|
|
||
| public Long getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public void setNumber(Long number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| LongIdDomain that = (LongIdDomain) o; | ||
| return Objects.equals(number, that.number) | ||
| && Objects.equals(name, that.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(number, name); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "LongIdDomain{" | ||
| + "number=" | ||
| + number | ||
| + ", name='" | ||
| + name | ||
| + '\'' | ||
| + '}'; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
| package com.azure.spring.data.cosmos.domain; | ||
|
|
||
| import com.azure.spring.data.cosmos.core.mapping.Document; | ||
| import com.azure.spring.data.cosmos.core.mapping.PartitionKey; | ||
| import org.springframework.data.annotation.Id; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @Document | ||
| public class LongIdDomainPartition { | ||
|
|
||
| @Id | ||
| private Long number; | ||
|
|
||
| @PartitionKey | ||
| private String name; | ||
|
|
||
| public LongIdDomainPartition(Long number, String name) { | ||
| this.number = number; | ||
| this.name = name; | ||
| } | ||
|
|
||
| public LongIdDomainPartition() { | ||
| } | ||
|
|
||
| public Long getNumber() { | ||
| return number; | ||
| } | ||
|
|
||
| public void setNumber(Long number) { | ||
| this.number = number; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| LongIdDomainPartition that = (LongIdDomainPartition) o; | ||
| return Objects.equals(number, that.number) | ||
| && Objects.equals(name, that.name); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(number, name); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "LongIdDomain{" | ||
| + "number=" | ||
| + number | ||
| + ", name='" | ||
| + name | ||
| + '\'' | ||
| + '}'; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should return type, why is it returning Object ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@moarychan - I have pushed a commit to fix this.