I am migrating a old project to a new tech stack without changing the database model(I know that is bad but no option). So I already have a audit table defined which has data as well.
Can I use the same audit table with Hibernate envers audit framework?
For other CRUD operations I am using spring data JPA.
what would be the best option for auditing? I want to audit delete as well.
You can not use existing audit tables as Audit tables created by envers have a specific format. If your existing audit tables follow the same pattern then you can use envers annotations to map table name and column names of your audit tables to envers tables.
You can refer docs for more info.
Related
I am using a Postgresql database with JPA / Hibernate. When I'm adding a constraint to a column, i.e. "nullable=false", the database column is not altered to reflect this. Deleting the table and rerunning the application does the job.
Can this be achieved with JPA/Hibernate - mechanisms only WITHOUT deleting entries or the table? Like "Try to alter the table and refuse to do so on inconsistent data"? In my application.properties, I've set
hibernate.hbm2ddl.auto=update
Any other setting seems to be deleting data and/or tables.
A working solution would be to run an ALTER TABLE script and adding a constraint annotation accordingly, but I'm not really fond of this.
It is impossible to add constraints to the existing table. Think wide about it - what if the data in the table is not satisfied with the constraint? You will get the whole application crash. So Hibernate not even trying to apply the potential harmful operation.
In general, there is the wrong way to manage database schema with hibernate. Auto-generation schema is appropriate for learning or MVP purposes but not for production. Also, SQL migration is a bad idea for many reasons. You should use a special tool for schema management: liquibase (better for me) or flywaydb
with the property spring.jpa.hibernate.ddl-auto it's possible to auto generated table from entities.
What I need is that only some specific entities should be generated to tables in database an the rest of the entities do nothing.
It this possible ?
I guess this is your question here? https://discourse.hibernate.org/t/i-would-really-like-to-know-more-about-the-update/5455
Like I wrote there already, you shouldn't use hbm2ddl for anything other than development purposes. Use a proper schema management tool like Liquibase or Flyway.
As #pcsutar already commented, impl said SchemaFilterProvider
Is there a way to define with hibernate which entities should be created in which tenant? Because for different tenant, tables are not the same.
And second question, is it possible to configure (also with hibernate) that I want to get access to tenant2 tables from tenant1.
I use embedded h2 database. I would like to automatically create tables with hibernate.hbm2ddl.auto and fill these tables with flyway migration files.
Well, I'm using a Multitenant Architecture in Spring and I can help you with what I'm doing.
1: If you have different tables for different tenants you could either use a TenantFilter over your Entity or you can define your customized schema in schema.sql and use JdbcTemplate to execute the SQL.
I would prefer the second option over the first because it guarantees the correct schema creation.
2: You can make a switch to your tenant2 from tenant1 using a TenantContext and once you're done with your processing you can switch back to tenant1.
There are many demo projects on multitenancy with spring boot on GitHub you can have a look there.
I am working on a project where I need to create history of particular table automatically. I have used #Audited annotation for it but it's creating duplicate data in table.
I am running out of space due to duplicate data.Even EclipseLink is not sloving my problem.
You tagged your question with Hibernate, but if switching to Eclipselink would be an option for you, be sure to check out the History Policy feature of Eclipselink. It allows automatic historization of data.
Please check Hibernate Envers.
This component integrates with JPA and Hibernate, and takes care of maintaing a revision history for any #Audited annotated Entity.
There are some guides on the net:
Maintain the data versioning info with Spring Data — Envers
Spring Boot : How to add JPA and Hibernate Envers Auditing
Spring Boot + Hibernate + Hibernate Envers
I use JPA annotations (Hibernate implementation) to initialize my DB schema. And i follow the article DYNAMIC DATASOURCE ROUTING to implement the dynamic datasource routing class.
However, i have two databases (mapped 2 data sources). I set the first data source as defaultTargetDataSource. then start my application. When my application try to access 2nd data source, it tell me the table doesn't exist. It seems AbstractRoutingDataSource only create the table for the default data source but other data sources.
Is there any idea to create schema in all databases ?
PS.I'm using AbstractRoutingDataSource to implement my own DB shards.
I guess that you are using the hibenate configuration:
spring:
jpa:
hibernate:
ddl-auto: update
to reflect the entity changes to the database schema. This works fine as long as we use a single data source that is configured to be connected at startup.
However, if you have multiple data sources it is not possible to use this feature. The general approach with AbstractRoutingDataSource is to not have a data source at startup but select it at runtime.
If you select a primary data source, then it will be only applied to the primary one as hibernates applies this feature at startup, but the remaining databases will not be migrated.
To reflect the changes to all of your databases you can use a database migration tool such as Flyway or Liquibase.
Flyway is using SQL and pretty easy to configure and use to use.