#CreationTimestamp in Hibernate is storing wrong date in database - postgresql

I have used hibernate #CreationTimestamp (import org.hibernate.annotations.CreationTimestamp;) in my spring boot project for the field private Date createdDate;
and Date is of import java.util.Date; to store the default current timestamp. This date is not having any setter method in java code.
I am using PostgreSQL 9.5.4 version and when I save, the created date is saved as 28th October 2022 but the correct created date which should have been stored is 23rd January 2023. But #UpdateTimestamp is working correctly. The picture from the database depicts the same.
#Entity
#Audited
public class EntityClassName implements Serializable{
private static final long serialVersionUID = 6004021640401026397L;
...
private String createdBy;
#CreationTimestamp
private Date createdDate;
private String updatedBy;
#UpdateTimestamp
private Date updatedDate;
}
Update 1:
So when I use #Temporal(TemporalType.TIMESTAMP) it stores the correct date in the database. Please find below code snippet:
#CreationTimestamp
#Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
But in the same database schema for other tables, I haven't used #Temporal(TemporalType.TIMESTAMP) and it is still working fine and storing the correct date in the database.
Can someone please guide what is going wrong here? Appreciate your help

Related

how to delete qa database older than 2 months in mongodb using reactivemongorepository

I want to delete QA database older than 2 months in mongodb using reactiveMongoRepository using spring web flux, how can I do that, do I have to create a custom query a custom method can work. I have class in which particular fields are there
class A { private String Id;
private String title;
private Instant startTime;
private Instant endTime;
}
I am not getting idea how to work on that

Document in MongoRepository not deleted with 'expireAfterSeconds'

I would like my MongoRepository in Spring Boot to automatically delete documents at a certain point in time after creation. Therefore I created the following class:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
public class MyDocument {
#Id
private String id;
#Indexed
private String name;
#Indexed(expireAfterSeconds = 0)
private LocalDateTime deleteAt;
}
Then, I save it in the Spring Boot MongoRepository:
MyDocument doc = modelMapper.map(myDocumentDto, MyDocument.class);
LocalDateTime timePoint = LocalDateTime.now();
timePoint = timePoint.plusMinutes(1L);
doc.setDeleteAt(timePoint);
docRepository.save(doc);
I periodically query the repository and would assume that after one minute, the document will not be there anymore. Unfortunately, I get the document every time I query and it is never deleted.
What am I doing wrong?
The document is persisted as follows (.toString()):
MyDocument{id='5915c65a2e9b694ac8ff8b67', name='string', deleteAt=2017-05-12T16:28:38.787}
Is MongoDB possibly unable to read and process the LocalDateTime format? I'm using org.springframework.data:spring-data-mongodb:1.10.1.RELEASE, so JSR-310 should already be supported as announced in 2015 here: https://spring.io/blog/2015/03/26/what-s-new-in-spring-data-fowler
I could fix the issue:
First of all, java.time.LocalDateTime is no problem with Spring Data / MongoDB.
For clarification, add a name to an index, e.g. #Indexed(name = "deleteAt", expireAfterSeconds = 0). This step might not be needed though. However, adding the #Document annotation helped a lot:
#Document(collection = "expiringDocument")
When I delete the whole collection while my application is running, a new document insert will create the collection again but without the indexes. To ensure indexes are created, restart the application.

Spring Data MongoDB date format issue

I am having a problem handling dates between my java server and mongodb. I am using Spring Data with MongoDB. My java code is deployed in Tomcat on a linux environment.
I have verified that the server time on my Java linux box is the same as the linux box on which MongoDB is running.
A simple example to find an object gives me a discrepancy in the last modified date field.
public Note getNoteById(String noteId) {
Query q = new Query().addCriteria(Criteria.where("_id").is(noteId));
return mongoTemplate.findOne(q, Note.class);
}
In the Note class, I have a last modified field which is a java Date. However, when I retrieve the java object, I see that the last modified date is 6-7 hours different from the Database value.
#Document(collection = "notes")
public class Note {
#Indexed
private String id;
private String title;
private Date lastModifiedDate;
...
The database value for the last modified date field is ISODate("2016-05-09T18:51:02.000Z"). However, when I read the Note document in Java, I see that the Note object in Java has a date of Mon May 09 11:51:02 PDT 2016 which is 7 hours different from the DB value.
Can you please help?

JPA #PrePersist & LockModeType.OPTIMISTIC_FORCE_INCREMENT

I came up with interesting situation that I already know how to work around, but I was wondering if there is some elegant solution for this.
I have an Entity, which can not have a #Versio field since it is based on a legacy database, and the table has no column to have this kind of value.
Basically it is something like this:
#Entity
public class MyEntity {
#Id
private int id;
#Temporal(TemporalType.DATE)
private java.util.Date lastUpdated;
}
This is basically just for EULA (End User License Agreement) checking.
I want the Date to be updated when the eula has to be re-accepted (The new eula date is got from other place).
For that I was planning to use:
#PrePersist
#PreUpdate
protected void setPersistTime() {
this.lastUpdated = new Date();
}
The #PrePersist is called correctly when the entity is stored for the first time, but on the subsequent times the JPA seems to think that the entity is the same as before and the #PreUpdate won't be called as there is nothing to change.
I was planning to use
em.refresh(myEntity, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
But that won't work without the #Version which I cannot use due to the legacy db. (no version field I could use and the Date is of wrong type for it).
Btw. Using EclipseLink.

JPA Composite Primary Key generating

I have simple entity class (irrelevant methods omitted):
#Entity
#Table(name="CONNECTIONS")
public class Connection implements Serializable {
#Id private Long id_track;
#Id private Long id_carrier;
#Id private Date date_out;
#Id private Time time_out;
private Date date_in;
private Time time_in;
private Double price;
...
}
I expect that JPA (in my case Eclipse implementation) creates TABLE CONNETIONS with composite primary key that consists of id_track, id_carrier, date_out and time_out columns but it adds addidional column id (of type integer) What do I do wrong?
I can not reproduce this. Are you sure JPA is creating your table?
Also ensure you have recompiled and redeployed your code.
Perhaps enable logging, and include what JPA provider and version you are using.
Are you using Glassfish?