Sachin Bose
Sachin Bose

Reputation: 157

How to update Auto generated time stamp in Spring boot

I am making use of Auditing with Spring Data JPA to automatically update the created by time stamp. I have a requirement, where i have to migrate some data for which the created by date should be updated with the value am setting through the application. I have debugged the code, the values are setting properly. But while calling

fooRepository.save(fooEntity)

The created by is getting overridden by the auto generated time stamp. Is it possible to override the value?

I am also having a @Transactional annotation in Service level. While debugging i can see the date is getting replaced on the Entity returned by the save repository method.

Upvotes: 1

Views: 1364

Answers (2)

Eklavya
Eklavya

Reputation: 18430

It is not a better way but you can follow. First, create the entity

fooRepository.saveAndFlush(fooEntity);

and then update the createdAt data then save the entity again.

fooEntity.setCreatedAt(yourTime);
fooRepository.saveAndFlush(fooEntity);

Upvotes: 1

Do Trung Duc
Do Trung Duc

Reputation: 406

Create new enity without @CreatedDate and new repository for that new entity.

Only use that new repository when you need to edit CreatedDate or LastModifiedDate

This seems to be a bad solution but is the only solution I can think of now.

I am looking for better solution to this problem

Upvotes: 0

Related Questions