Javers Mongo collections are not initialized on startup - spring

I am using Javers 6.5.3 with MongoDB with transactions. When I am trying to commit changes in Javers I have got an error:
Cannot create namespace jv_snapshots in multi-document transaction
To fix it I have to manually create jv_snapshots and jv_head_id collections in my database, but I am loosing all the indexes and stuff from Javers MongoSchemaManager
I was trying to add #EnableMongoRepositories({"org.javers.spring.repository"}) in config file as shown in documentation but it does not work.
How to force Javers to create collections on start up?
Thank you

Related

A way to create history table with Hibernate ddl-auto

I am trying to create a system-versioning (history) table in SQL-server via SpringBoot Hibernate entity, but I would like to make automatic creating via ddl-auto.
Is there a way to make it? all I found is Envers library with #Audited annotation, but it creates a new table not a system-versioning one.

Spring boot hibernate application throws Negative revision numbers are not allowed when inserting

I have taken a DB dump from my dev database to QA database ( oracle) for testing. My application is a spring boot application and uses hibernate envers for auditing. I get above error when trying to insert data to the tables. I tried removing data from all the audit tables and revinfo table. But the issue is still there. Anybody has any idea on this?
Same error after updating the version of hibernate-envers for springboot3+. One quick solution for me was to (first backup) remove all tables related to audit + remove revinfo table and also the sequencer (revinfo_seq if you created some). Then let the ddl-auto property do the work by setting in application.yml
jpa.hibernate.ddl-auto: update
It creates all needed tables and sequencers by its needed definition, and after that inserts were committed.

I need to generate Liquibase Change-set for my JPA entities. How can I do that?

I have tried the following
liquibase:generateChangeLog - It generated the change log from my db.I need to generate the change-log from my JPA entities.
liquibase:diff - It generates the change log for the difference between my db and JPA entities. I cannot say that my db is always empty and I want to generate the create scripts which can be applied on fresh db.
How can I use Liquibase to generate the scripts based on my JPA entities only ?
Note : I am ok in providing the details about my db such as url,driver etc
If your IDE of choice is IntelliJ IDEA, I'd recommend using the JPA Buddy plugin to do this. It can generate Liquibase changelogs by comparing your Java model to the target DB.
So if your DB is empty, you'll get a changelog that describes your whole model. But it is also useful to keep your evolving model and your changelogs in sync.
Once you have it installed and have Liquibase as your Maven/Gradle dependency, you can generate a changelog like this:
Try to use liquibase-hibernate-plugin
You have to create a schema with persistence properties according to Database Schema Creation and then use the Liquibase generateChangeLog command.

Database table creation and updation with springboot, spring data

For the database schema management with spring data/hibernate, setting spring.jpa.hibernate.ddl-auto option doesn't look like a cleaner approach.
Bcoz
1) We are forced to put the credentials of a user that has permission to create and delete in the application.properties.
2) In production, relaying on spring.jpa.hibernate.ddl-auto option could lead to dataloss, if not managed carefully.
So what is the best way out there to handle the database schema management automatically in a spring boot/spring data app?
Any pointers would help.
Thanks
If you want to track each change state of database then you can use flyway
See this link how to maintain database versioning in spring-boot
In production, you should ideally set spring.jpa.hibernate.ddl-auto property as none so that no schema changes are allowed.
For the database schema management with spring data/hibernate, I would suggest you go for Liquibase, it basically is an open source database-independent library for tracking, managing and applying database schema changes.
Every change to Schema is added as a changeset using property file in Liquibase , this is for the new changes.
In order to migrate the existing database structure into Liquibase, it provides you with commands to automatically generate Changesets by reading the current database.
So using this you can generate database schema, add constraints, load data.
More info at : https://www.liquibase.org/ , Why and when Liquibase?

Javers - What are advantages of using Javers instead of Envers?

I am developing a RESTful API using Spring Data REST. Now for auditing, Spring does have the option to auditing meta data like created_date and modified_date but they don't provide entity versioning.
Currently there are two popular libraries for entity version which are Envers and Javers. I have looked over for a comparison of both but there arent any articles on this matter.
So what are the benefits and drawbacks of using Javers over Envers?
There are two big difference 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 choosed NoSQL database or SQL but with other persistence framework like
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 data model is a copy of application’s data model. As the doc says:
For each audited entity, an audit table is created.
By default, the audit table name is created by adding a _AUD suffix to the original name.
It can be advantage, you have audit data close to your live data. Envers’ tables look familiar.
It’s easy to query them with SQL.
JaVers uses its own Snapshot model for audit data.
Snapshots are decoupled from live data,
JaVers saves them to the single table (jv_snapshots) as JSON documents with unified structure.
Advantages? You can choose where to store audit data.
By default JaVers uses the same database as application does,
but you can point another database. For example, SQL for application and MongoDB for JaVers
or centralized JaVers database shared for all applications in your company).
Read this blogpost with full JaVers vs Envers comparison:
https://javers.org/blog/2017/12/javers-vs-envers-comparision.html
Enver is like git for a database.
I do not know Javers but a complete Envers databinding has this advantages:
A table is created in the database called REVINFO having a timestamp and a PK.
To every entity that is audited, one shadow-copy is created. Theese shadow-copies have every field nullable and the PK is not a PK. Theese shadow-copies have a new field, the reference to the table REVINFO.
This gives Enver the possibility to record changes that has been made in the past in this shadow-copies. You can move that shadow-tables into an different database.

Resources