EventListener like AbstractMongoEventListener in Spring Data JPA? - spring-data

We are migrating from a Spring Data MongoDB repository to a Spring Data JPA repository. We were using the AbstractMongoEventListener to capture onBeforeConvert and onAfterLoad events to enhance the data objects before returning them from our Service layer.
I cannot find similar EventListeners in Spring Data JPA. Are there hooks in Spring Data JPA that I can use to do the same thing? It would be great if I can avoid modifying our service layer to make this change from MongoDB to JPA.
Thanks!

The #PrePersist annotation is exactly what I was looking for.

Related

How to keep a history of edit of Entities in a JPA application

A JavaEE and JPA application need to keep a record of all the changes made by the user.
Currently, for all the entities, there are fields to record createdBy and lastEditedBy properties. Yet, the requirement of recording all edits is not possible with those properties.
What is the best way to record the history of all edits for a particular entity?
I do not use Spring.
You can use Javers which is db and framework agnostic tool for maintaining operation history.
There are two big differences between JaVers and Envers:
Envers is the Hibernate plugin. It has good integration with Hibernate
but you can use it only with traditional SQL databases. If you chose
NoSQL database or SQL but with another persistence framework (for
example JOOQ) — Envers is not an option.
On the contrary, JaVers can be used with any kind of database and any
kind of persistence framework. For now, JaVers comes with repository
implementations for MongoDB and popular SQL databases. Other databases
(like Cassandra, Elastic) might be added in the future.
Envers’ audit model is table-oriented. You can think about Envers as
the tool for versioning database records.
JaVers’ audit model is object-oriented. It’s all about objects’
Snapshots. JaVers saves them to the single table (or the collection in
Mongo) as JSON documents with unified structure.
You can also achieve this using triggers and storing object differences.
Edit:
JaversAuditableAspect for any kind of repository.
It defines the pointcut on any method annotated with the method-level #JaversAuditable annotation. Choose it if you have repositories that are not managed by Spring Data.
#Bean public JaversAuditableAspect javersAuditableAspect() { return new JaversAuditableAspect(javers(), authorProvider(), commitPropertiesProvider()); }
You can use Hibernate's Envers to audit your entities. It allow you to keep track of ALL changes made to entities - even deleted ones. Most probably you are already using Hibernates (as JPA provider) so integration should be a no problem.
https://hibernate.org/orm/envers/

Is it possible to use hibernate envers #Audited with r2dbc?

I hope to achieve auditing of my tables using #Audited annotation of the Hibernate Envers project while I'm using Spring Data R2DBC or plain R2DBC to insert data into my tables.
Is this even feasible. I am not able to tell if Spring Data R2DBC uses hibernate at all.
I tried adding the #Audited annotation to my entity class to no avail.
this.fnclInfoRepository.save(itfnclinfo).subscribe();
#Data
#Builder
#Audited
public class Itfnclinfo implements Persistable<String> {
#Id
private String fnclInfoId;
..
}
I was expecting a new table created by hibernate with the suffix _AUD that holds the copy of all inserted data
I realize that Spring Data JPA is an abstraction over Hibernate and therefore Spring Data R2DBC is not a JPA provider abstraction and therefore not related to Hibernate. I'll have to come up with another way for auditing.
Spring data envers only works with traditional Spring Data JPA.
In a Spring Boot project, you can customize your AuditEvent and AuditEventRepository and setup the change log manually.
Check the Spring Boot docs - Auditing.
It maybe need more extra work, the good part is the auditevent is integrated with Spring Boot Actuator, you can track the auditevents by Actuator urls.

Spring data JPA get child collection attributes using projections

I would like to fetch only few attributes from child collection entitites. So, is it possible using Spring JPA projections and Spring JPA repository?
Also, it should not execute multiple queries. Just one query to fetch the selected attributes form child collection.
I want to evaluate options first if it is available in Spring JPA before considering any other alternative.

Spring Data MongoDB prevent persisting certain fields

I am using Spring Data for MongoDB to persist my domain objects. I was wondering if there is a way (perhaps with an Annotation?) to prevent Spring Data from persisting certain fields into MongoDB?
Does someone know how to do that or do I have to write my own Mapper?
Thanks.
In this case use the #Transient annotation for the field you need to ignore.
Look more over here - Transient
In case you are looking for the actual package like I was, this one will work:
import org.springframework.data.annotation.Transient;
Which is from the Spring framework API documentation.
But this one, which is a JPA annotation, will not work for Spring Data's MongoDB:
import javax.persistence.Transient;
Which is part of the Java Persistence API.

Auditing with Spring Data JPA

I am using Spring Data JPA in an application in which all entity objects need auditing. I know that I can have each either implement Auditable or extend AbstractAuditable, but my problem is coming with the overall auditing implementation.
The example on the Spring Data JPA reference pages seems to indicate that you need an AuditableAware bean for each entity. Is there any way to avoid this extra code and handle it in one place or through one configuration?
The generic parameter of AuditorAware is not the entity you want to capture the auditing information for but rather the creating/modifying one. So it will typically be the user currently logged in or the like.