Issue
I'm supporter one code that have a lot of spread
dummyRepository.save(dummy)
but I need do some operations with "dummy" object before save, how can I do this?
Solution
I would have a look at the annotations of the Java Persistence API (JPA):
@PrePersist
Executed before the entity manager persist operation is actually executed or cascaded. This call is synchronous with the persist operation.
and
@PreUpdate
Executed before the database UPDATE operation.
There are various ways to use them:
This annotation may be applied to methods of an entity class, a mapped superclass, or a callback listener class.
The easiest way is to just add a Method in your Entity with this annotation. For example:
@PrePersist
@PreUpdate
protected void onCreateOrUpdate()
{
setLastChangeDate(new Date());
}
Answered By - Andreas Radauer
Answer Checked By - Candace Johnson (JavaFixing Volunteer)