Skip to content
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

DateTimeFormat not supporting #29

Closed
jAddict7 opened this issue Jun 9, 2014 · 3 comments
Closed

DateTimeFormat not supporting #29

jAddict7 opened this issue Jun 9, 2014 · 3 comments

Comments

@jAddict7
Copy link

jAddict7 commented Jun 9, 2014

In repository,
public interface TestRepository extends PagingAndSortingRepository<Test, String>{
public Page findByNameAndDate(@param("name") String name,
@param("date") @DateTimeFormat(iso = ISO.DATE) Date date, Pageable pageable);
}

Here Date not getting converted to, whenever I pass its showing null cannot be accepted and also I tried formatting with @DateTimeFormat(iso = ISO.DATE, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"). But not working.
My stackoverfow link --> http://stackoverflow.com/questions/24096924/how-to-format-param-string-in-spring-data-rest

@michaellavelle
Copy link
Owner

Hi

DynamoDB stores dates as Strings, so if you have a date attribute as part of your date model you can either represent them as Strings in your data model ( as I think you are doing currently ), or you can represent them as Dates, and configure Spring-Data-DynamoDB so that it maps the Dates to Strings. This can be done using Custom Marshallers from amazon-aws-sdk, and support has been added to handle this in the Spring Data DynamoDB module.

You can read about marshallers here : http://java.awsblog.com/post/Tx1K7U34AOZBLJ2/Using-Custom-Marshallers-to-Store-Complex-Objects-in-Amazon-DynamoDB

Note that this marshalling is separate from any mapping that you may be requiring Spring-Data-Rest to perform from JSON to objects - for this you will still need the @DateTimeFormat annotation.

If you want to represent the date as a java.util.Date in your data model, simply annotate the getter for the attribute in your domain class with @DynamoDBMarshalling, and pass in the class of marshaller you wish to use, eg:

@DynamoDBRangeKey(attributeName = "ReplyDateTime")
@DynamoDBMarshalling(marshallerClass=DefaultDynamoDBDateMarshaller.class)
public Date getReplyDateTime() {
...

DefaultDynamoDBDateMarshaller here is a support class from Spring-Data-DynamoDB, but you can implement your own for custom date/string mapping.

With this in place, you can now change your repository finder methods so they expect Date parameters rather than Strings:

public Page findByReplyDateTimeAfter(Date replyDateTime,Pageable pageable);

Hope this helps,

Cheers,

Michael

@jAddict7
Copy link
Author

jAddict7 commented Jun 9, 2014

Thank you Michael. I am unable to use DynamoDBRangeKey, Is there any working example, am getting exception when I include range key annotation(@DynamoDBRangeKey). Exception caused is,

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No method annotated by DynamoDBHashKey within type java.lang.String!

java.lang.IllegalArgumentException: No method annotated by DynamoDBHashKey within type java.lang.String!

@michaellavelle
Copy link
Owner

There is a working example of range key at
https://github.com/michaellavelle/spring-data-dynamodb-demo

If you are using a hash and range key table ( composite id) , in order to use Spring-Data-Dynamodb, you'll need to create a separate composite id class with getters and setters for the component parts, where the getters are annotated with @DynamoDBHashKey and @DynamoDBRangeKey respectively.

( eg https://github.com/michaellavelle/spring-data-dynamodb-demo/blob/master/src/main/java/org/socialsignin/springframework/data/dynamodb/demo/domain/ReplyId.java )

Then setup your domain class so that it has an id field whose type is this new composite id class, and annotate the field with @id.

(eg. https://github.com/michaellavelle/spring-data-dynamodb-demo/blob/master/src/main/java/org/socialsignin/springframework/data/dynamodb/demo/domain/Reply.java )

Finally, ensure you have getters and setters for the component parts of the composite key in the domain class, with the getters annotated with @DynamoDBHashKey and @DynamoDBRangeKey ( the same methods as you created in the Id class ), but modify these methods so they manipulate the corresponding values of the Id field.

https://github.com/michaellavelle/spring-data-dynamodb-demo/blob/master/src/main/java/org/socialsignin/springframework/data/dynamodb/demo/domain/Reply.java

michaellavelle pushed a commit that referenced this issue Jul 13, 2020
* Added Filter Expressions for Queries

* More test coverage.

* Remove duplicate code for Scan

* Fixing bad checks, updating test coverage.

* Fixing some warnings, added deprecation warning

* Supported for named parameters

* Supported for named parameters

* Empty check on annotations.

* Bumping test coverage

* Bumping test coverage

* Bumping test coverage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants