A way to create history table with Hibernate ddl-auto - spring-boot

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.

Related

Prevent specific #Entities to generate table - hibernate JPA

Is it possible to stop spring JPA from generating table for a specific entity ?
Reason:
I want to put all my native sql queries into a separate object using #NamedNativeQuery. But #NameNativeQuery requires #Entity annotation. Due to that, and unwanted table is generated automatically.
Is it possible to add an #Entity, and stop at the same time spring boot to generate table for that entity ?
P.S: I don't want to put the queries into the parent #entity, because there are too many queries. I want to have a good model structure.
My settings :
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none
Spring Boot version : 2.3.3
Hibernate version : 5.6.7.Final

Spring Boot h2 initial data not creating table first

I am trying to create a small Spring Boot setup with a h2 database.
Now I have a weird problem, which I can't solve.
If I don't create an data.sql for initial data, the app starts fine and creates my entity tables.
If I create an data.sql for initial data and keep the existing table from previous step, everything works fine.
If I create an data.sql for initial data and remove my existing h2 file, I get the error that it can't import the data, because the table is missing.
How do I tell Spring to create my tables before importing the initial data?
This is covered in the release notes for Spring Boot 2.5:
By default, data.sql scripts are now run before Hibernate is initialized. This aligns the behavior of basic script-based initialization with that of Flyway and Liquibase. If you want to use data.sql to populate a schema created by Hibernate, set spring.jpa.defer-datasource-initialization to true. While mixing database initialization technologies is not recommended, this will also allow you to use a schema.sql script to build upon a Hibernate-created schema before it’s populated via data.sql.

JHipster create new Table entity without using initial schema

I recentely started using JHipster. I am facing problem while creating new tables (other than the jhipster provided). JHipster provided their tables through 00000000000000_initial_schema.xml even though same table has entity java classes.
But now i have created new #Entity class but tables are not getting created. What changes need to do ?
One more thing to ask, how we can disable initial schema totally and generate tables from Entity classes, even for existing jhi_* tables
Thanks in advance :)

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.

Need to build a GUI using hibernate and spring where we can update/alter schema structures from user interface

Please guide me on this how to update entity classes when schema is updated and how to make sure the entity classes and table schema are in sink.
Scenario: User can add a new column by clicking add column button from screen.
How do i update schema using hibernate?

Resources