Update timestamp in dynamoDB - java

I want CREATE_DATE and LAST_UPDATED fields in DynamoDB table. It is creating those fields with initial values for both fields as current date. But when some of the columns get updated it does not update the LAST_UPDATED column with new date.
I am using DynamoDBAutoGeneratedTimestamp annotation and both CREATE_DATE and LAST_UPDATED are of type java.util.date
#DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.CREATE)
private Date CREATE_DATE;
#DynamoDBAutoGeneratedTimestamp(strategy=DynamoDBAutoGenerateStrategy.ALWAYS)
private Date LAST_UPDATED;
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/datamodeling/DynamoDBAutoGeneratedTimestamp.html.
Can anyone tell what else needs to be added if I am missing anything ?
Or I need to handle update date in code and it would not be auto populated?
Thank you

Related

localdate value don't return result [duplicate]

This question already has answers here:
How to persist LocalDate with JPA?
(5 answers)
Closed 2 years ago.
I use Java 11, spring boot 2.3.4 with jpa (hibernate implementation)
I have an entity
public class Prba{
Long prId;
Long baId;
LocalDate startDate;
}
My jpql query
select
prba from Prba prba
where
prba.prId != :prId
and
prba.baId = :baId
and
prba.startDate = :date
baId, prId are long (number(18,0) in oracle)
date is localdate (date in oracle)
query generated
select * from prba
where
prba.pr_id != ?
and
prba.ba_id = ?
and
prba.start_date = ?
I get no result
Without start_date condition I get a row, start_value is 15-08-2020, it's the java value i pass, but i don't get any result
Edit
if i use
to_date(prba.start_date, 'DD-MM-YYYY) = TO_DATE ('15-08-20', 'DD-MM-YYYY')
that work
Is there any method to use in jpa when I use localdate
Edit 2
create table prba (
pr_id number(18,0),
ba_id number(18,0),
startDate date
)
Issue it's in orale a time is saved with a date, if you pass by a ui tool to insert data, 00:00:00 for the time will not be used and if you compare you will get wrong date
You need to allow for the fact that in oracle a DATE data type includes the time down to the second. So if the date in your oracle table has, effectively '23-Sep-2020 15:24:32', and the date you are comparing it to is just a date with no time, then they will not match. Everything you've presented - especially the use of the to_date to correct it, suggests that is your issue. BTW,
Here's a demo of what I just said, with an example of the normal adjustment to deal with DATE and ignore the time component:
SQL> -- create and populate test table
SQL> create table my_test (dob date);
Table created.
SQL> insert into my_test values (sysdate);
1 row created.
SQL> -- unconditional select to prove what's there
SQL> select dob,
2 to_char(dob,'dd-mon-yyyy hh24:mi:ss') date_time
3 from my_test;
DOB DATE_TIME
--------- --------------------
23-SEP-20 23-sep-2020 15:07:50
1 row selected.
SQL> -- use where clause that does not account for the time component, it returns nothing
SQL> select dob,
2 to_char(dob,'dd-mon-yyyy hh24:mi:ss') date_time
3 from my_test
4 where dob = to_date('23-09-2020','dd-mm-yyyy')
5 ;
no rows selected
SQL> -- use trun() to eliminate the time component
SQL> select dob,
2 to_char(dob,'dd-mon-yyyy hh24:mi:ss') date_time
3 from my_test
4 where trunc(dob) = to_date('23-09-2020','dd-mm-yyyy')
5 ;
DOB DATE_TIME
--------- --------------------
23-SEP-20 23-sep-2020 15:07:50
1 row selected.
SQL> -- clean up the test
SQL> drop table my_test purge;
Table dropped.
SQL> spo off
Some other observations:
your use of "to_date(prba.start_date, 'DD-MM-YYYY)" ... the to_date function take a string as its input. So if prba.start_date is of data type DATE, you force oracle to first do an implied to_char to make it the string required by to_date. And it is only a matter of time before that comes back to bite you, due to conflicting NLS_ settings. And if prba.start_date is NOT of data type DATE, then that is in itself a design failure.
I'd suggest you give some thought to your naming conventions.
select
prba from Prba prba
you have a table name PRBA, in that table you have a column also named PRBA. Not a good idea. Spend a little time researching column and table naming conventions.

compare date from database with value i get from inputtext in java jsp and sql

I have a table , there are 4 fields: name, start date and time (Timestamp) ,end date and time (timpestamp) , car.
In Java/MySQL,
I need to compare database start date time and end start date time and compare with value given in textbox date time field .
Now problem is that we need to book person(driver) and car, if car and person
are not booked in given time (that is checked by database) , then we can booked it else not.
Please tell me a logic/query to do this.
If you have code please mention it.
Try this query,
SELECT name FROM table having '2014-11-28' between start_date and end_date;
You can use this query to check availability.
SELECT name FROM table WHERE start_date < '2014-03-13 11:42:28' AND end_date > '2014-03-13 11:42:28' limit 1
If you get any name, then you can not book new driver. Also look at mysql date and time functions.

Hibernate insert default datetime to a not null column

In my table I have a column with following definition.
createdat DATETIME NOT NULL DEFAULT NOW(),
I'm trying to insert data in to this table using hibernate. I want this column to have default value. But it gives me exception
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'createdat' cannot be null
Any suggestions?
Just initiate your date object with new Date object in your POJO which is mapped in hibernate.
java.util.Date date = new java.util.Date();
I suppose you have your setters and getter for POJO, whenever you forgot or dont want to set date object this default date will be used

How to convert java.util.Date into current time in timestamp..?

i am making a program in java.
i am using the following code
u.setLastlogin(new java.util.Date());
above function accepts parameter as java.util.Date but i want to store this value in a database table where the column is of type timestamp?
can any one help how to code so that i can insert the current timestamp of the system in the table. thanks.
You can convert Date to Timestamp as follows:
Timestamp timestamp = new Timestamp(date.getTime());
And if you want timestamp of current date:
new Timestamp(System.currentTimeMillis())
Date now = new Date();
now.getTime();
With JPA:
annotate lastLogin with: #Temporal(TemporalType.TIMESTAMP)
With JDBC:
PreparedStatement#setTimestamp
use this to java.sql.Timestamp timestamp = new java.sql.Timestamp(new Date().getTime());
u.setLastlogin(timestamp);
You could also just issue an SQL statement:
UPDATE MY_TABLE SET LAST_LOGIN = NOW();
This is for MySQL. You should also find this helpful: MySQL Date and Time functions
EDIT
As Rambo requested, setting default value of a column to the actual timestamp (As per MySQL: ALTER TABLE syntax and MySQL Data type default values):
ALTER TABLE MY_TABLE ALTER MY_TIMESTAMP COLUMN SET DEFAULT CURRENT_TIMESTAMP;

Android content provider modified / created date

In example: If I'm accessing the SMS database, there is a 'Date' field for message timestamp. However, say I load messages with an older timestamp into the database, how do I know that they were added after the later timestamped messages? Is there a modified or created date for table rows that I can access?
Edit and clarification:
The SMS table has the following columns: long _id, thread_id, address, person, date, protocol, read, status, type, reply_path_present, subject, body, service_center, locked,error_code, & seen.
'date' refers to the date which the message was sent, not when it was added to the database. Does the Android database (SQLite) track when data is added or modified in the database? If so, how do I access it? If it does not, and simply add/removes/updates data without logging, that's an answer too.
you can solve this problem by creating a column containing the current date and time (current means at the moment, the row was inserted):
create table mytable(_id int primary key, date datetime default current_timestamp);
this will result in a timestamp in every row that points to the moment of insertion
another solution would be simply insert the current timestamp out of the SQLiteOpenHelper like:
ContentValues values = new ContentValues(2);
values.put(COLUMN_NAME_ID, id);
values.put(COLUMN_NAME_TITLE, title);
values.put(COLUMN_NAME_CREATED_DATE, System.currentTimeMillis());
dbHelper.getWritableDatabase().insert(TABLE_NAME, null, values);
There is no date in sqlite for when the row was inserted or updated.

Categories

Resources