Is there listeners available like postUpdate, postCreate in Javers - javers

Is there listeners available in javers which will be invoked before executing a specific operation.
For example, a method like preUpdate() which will be invoked before registering an update operation.
I am using javers with Mongo DB and Spring
PS: Looking for feature similar to conditional auditing in Hibernate Envers

There is no such feature in JaVers and no plans for adding it. You can implement these hooks in standard Spring AOP, for example by providing your implementation of JaversAuditableAspect.

Related

Hibernate session factory and repository confusion

So there are two ways to persist an entity:
Using Hibernate's session factory where we get the current session and call save(), get(), update() methods.
Extending JPA's repository interfaces
I have the following questions:
How are these two methods different in the context of using Hibernate. As far as I understand, Hibernate is an implementation of JPA API. So when I say I want to use Hibernate, does it mean that I can use both of the above methods?
What is the preferred way of the two based on convenience, flexibility and optimisation?
JPA repository behind the scenes uses Hibernate or JPA APIs to implement its functionality and tries to "abstract" it or provide convenience methods on top of it. You don't have to use JPA repositories though, and can always switch to the Hibernate or JPA APIs when needed. Think though, if you really gain any benefit by using the Spring Data JPA repository concept.

AuditAware with Kotlin Exposed

I have a project using Kotlin in Spring Boot. The tables have the standard audit fields 'createdBy', 'createdDt'. Is there a way to handle setting these fields in some way that doesn't involve passing these fields around? Ideally I'd like to intercept every transaction before commit and just set the fields on the entities.
Using JPA and Hibernate, an AuditAware bean could be used to pass the current Principal along with using annotations. Is there a similar integration available or easily implemented? I've looked at the EntityHooks class and don't see an easy way to change data.

Object/Entity lifecycles when using spring-data-jpa with hibernate as jpa provider?

When using spring-data-jpa with hibernate as jpa provider, are the Object/Entity lifecycles same as when using hibernate directly or as defined by hibernate (or might be jpa spec itself).
Hibernate defines these lifecycles to entities - Transient, Persistent, Detached, Removed.
Are these same life cycles applicable when using spring-data-jpa too.
If so how does below the methods provided by Hibernate map with the methods of spring jpa crud repository.
//below methods in hibernate move an entity to persistent state
save(e),
persist(e);
update(e);
saveOrUpdate(e);
lock(e);
merge(e);
and
//below methods in hibernate move an entity to detached state
detach(e);
evict(e);
For the first part of the question:
Spring Data JPA just offers some comfortable mechanics on top of JPA.
The persistence, mapping and life cycle is still managed by JPA or its implementation, i.e. Hibernate in your case.
This means the life cycle is the same.
As for the mapping between Spring Data JPAs methods and Hibernates/JPA methods see the following table.
Spring Data
JPA
CrudRepository.save*
for new entities EntityManager.persist, EntityManager.merge otherwise
CrudRepository.delete*
EntityManager.remove
CrudRepository.findById
EntityManager.find*
JpaRepository.*flush
EntityManager.flush
JpaRepository.getById
EntityManager.getReference
Other query methods predefined in interfaces or otherwise use various types of queries.
Spring Data Jpa is only an abstraction layer and not provide a lifecycle management. Therefore, if you are using hibernate as a jpa implementation your object's lifecycle will regulated according to hibernate's lifecycle management.
Also, you can find some other explanations here and here as well.

Pass Spring datasource to Javers to Audit DTOs

In My Spring Boot application, I would like to audit DTOs instead of entities and move audit logic to common library which will have #EnableAudit annotation to audit DTO at method level. If I am using JaversBuilder.javers().build(); in common library I would not be able to commit data as it would not know anything of datasource.
I want to create Javers instance in my application and pass datasource to it and then pass Javers instances to common library to perform commit. Is there anyway in Spring boot application to create Javers instance and associat datasource to it which will be used at the time of commit ?
If you take javers-spring-boot-starter, you will get a Javers instance created as a Spring bean. It will be connected to your database and ready-to-use. See https://javers.org/documentation/spring-boot-integration/

What is the point of using spring transaction while we have database level transaction?

What is the point of using spring transaction while we have database level transaction ?
If you use mysql, oracle or any other db in java, they provide methods to make things inside transaction then why spring transaction if I can do transaction using java DB driver methods ?
It's another layer of abstraction over the database transaction API. So if you wanted to use multiple databases with global transactions, Spring would allow you to do this. While I have never done it, I believe it would allow you to use DB2 and Hibernate together, for example.
Generally, what I've found is, if a feature is available in Spring, it's because there is a use case for it. They don't just put things into the framework without a reason.
Also, Spring provides declarative transaction demarcation, which produces more readable and maintainable Java code. The declarative approach lets us change the transaction strategy easily, without changing the code.
The Declarative transaction management approach allows you to manage the transaction with the help of configuration instead of hard coding in your source code. This means that you can separate transaction management from the business code. You only use annotations or XML based configuration to manage the transactions
We used Spring AOP along with Hibernate using this transaction strategy Here is an example, Spring AOP transaction mangement with Hibernate.

Resources