Deepu--Java
Deepu--Java

Reputation: 3820

How to write current timestamp in cassandra column using entity mapper

We are using entity mapper to map cassandra table to java object. Ex

class Activity
{
   @PrimaryKey
   int id;
   @Column
   Date modifiedDate;
   @Column
   String activityDesc;
}

I want to add modifiedDate as the current timestamp generated by cassandra node from function totimestamp(now())

How can I achieve this with mapper method without using plain CQL insert query. I checked the docs and found @Computed supports function but that is just for select queries.

Upvotes: 1

Views: 91

Answers (1)

Aaron
Aaron

Reputation: 57748

Date modifiedDate;

First, remember that Cassandra's DATE type maps to Java.time.LocalDate.

Second, when using a DB repository framework, you end up making a tradeoff of explicit control for the convenience of not having to build a boilerplate DAL. In this situation, I've often set the date at object creation time using LocalDate.now().

myDataEntity.setDate(LocalDate.now());

current timestamp generated by cassandra node

Third, this shouldn't be too much of a concern today. An enterprise's servers should be configured to the same NTP endpoint, which means that the date/time will be the same regardless of where it is generated.

I could maybe see if it was a full timestamp and milliseconds mattered. But even without NTP, a precision of date shouldn't vary much between an app and a database server.

Upvotes: 0

Related Questions