I've got a big number of entities.
For every entity I've got an interface that extends CrudRepository
.
Every entity is saved using *Repository.save(entity)
method.
And I want to implement a logger like that:
log
table:
logdata
table:
save
or delete
method runs, MyRepositoryTrigger should write a log message with all fields changed with field names and field values.But how to write smth like that?
So if the field is null in the entity to save , it will overwrite its value to null in the existing row.
In the repository interfaces, we can add the methods like findByCustomerNameAndPhone() (assuming customerName and phone are fields in the domain object). Then, Spring provides the implementation by implementing the above repository interface methods at runtime (during the application run).
Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, retrieval, search, update and delete operation on objects.
CrudRepository provides CRUD functions. PagingAndSortingRepository provides methods to do pagination and sort records. JpaRepository provides JPA related methods such as flushing the persistence context and delete records in a batch.
For trigger functionality you'll have to look at your DB but most likely it won't guarantee exclusivity of your CrudRepository
DB activity since it's set upon creation/deletion/update of table/column.
If instead you want to imitate trigger functionality meaning something to fire without you having to set it up every single time before/after your CrudRepository#save()/delete()
functionality then you are better off using Spring AOP
functionality
@After("execution(* my.CrudRepository.save(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " was called..");
}
You can find extensive Spring AOP programming documentation in the Spring Docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With