How to prevent Hibernate Envers for creating Audit tables? - java

In my case, i have a dependency to a module that implementing envers in its domains (its domains were annotated with #Audited). When i imported that module in my pom.xml and run my app, envers automatically creating audit tables based on its domains. My question, how can i prevent Envers for creating audited tables after the domains were annotated with #Audited? Is there any solution like adding a configuration in application.properties file or something like that?
org.hibernate.envers.table_creation = false

If you're importing a dependency that uses Envers but you do not require the tables or its functionality in your environment, you can easily disable Envers via a simple hibernate setting
hibernate.integration.envers.enabled=false
This should prevent Envers from bootstrapping and building any envers schema and it should also prevent the event listeners from being registered when entity model events fire where we track changes and generate the audit entries.

Related

Hibernate Multitenancy with different tables per tenant

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.

How to disable envers auditing for spring boot

i am creating a maven multimoduled project one of the module for the hibernate entity only , issue is two services/api/maven_project are using same module , but one requires auditing but other dont , how i can keep my code intact (means ,without changing or removing #Audited annotation) , how to enable or disable envers auditing at run time or compile time,
because after everything i have tried auditing is working for both api
i have tried
spring.jpa.properties.hibernate.integration.envers.enabled=false
spring.jpa.properties.hibernate.listeners.envers.autoRegister=false
spring.jpa.properties.hibernate.envers.autoRegisterListeners=false
hibernate.integration.envers.enabled=false
hibernate.listeners.envers.autoRegister=false
hibernate.envers.autoRegisterListeners=false
As per buræquete's answer
spring.jpa.properties.hibernate.integration.envers.enabled=false
Would do the job.
"Non-Spring Data JPA" Hibernate properties are configured through
spring.jpa.properties.hibernate.*

Tell Hibernate not to drop entities of library project

I have a library project called COMMON. COMMON is deployed to our company artifactory and is used by many other projects. In COMMON I have a few common entities. In one of the newer projects that does not yet have a database connection we see this error:
org.hibernate.tool.schema.internal.SchemaDropperImpl$DelayedDropActionImpl: HHH000478: Unsuccessful: drop table myschema.my_table if exists
This is frightening as these entities are not to be dropped. They are not even to be modified at line level as they are marked with #Immutable.
In the projects that use COMMON as a dependency I can tell hibernate not to do this with the following setting in application.properties:
spring.jpa.hibernate.ddl-auto=none
The problem with this is, that in project A if I want to use the ddl-auto feature on my A specific entities but not on the COMMON ones, I can't do that with this approach. Also if a dev forgets to add this property we drop the tables.
Can we restrict an Entity from being dropped/created in hibernate?

Java Framework to maintain entity history automatically

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

JPA entities with different databases - annotations for main database, XML for secondary database

I have two databases - new (refactored) and old (legacy) one and I would like the application to run for both databases (not at the same time, the intention is to run application on the old database for some period and then switch some clients to the new database and after some period to drop supprt for the old database). Apparently - the differente configuration, JPA entities-to-database mapping is the only difference.
I know that this can be achieved by maintaining two JPA persistence.xml configuration files. But I would like to have the following setting:
- configuration for the main database should be in annotations (because it is easier to develop and maintain annotations and thay can be placed in the main code).
- configuration for the old database should be in XML file that override annotations.
Is this possible and how it can be achieved. As far as I have heard, then the opposite approach is possible - to keep mapping in persistence.xml and use annotations for overriding.
Actually, the JPA providers let you define your mapping using annotations and override the settings using XML files. This is (almost) the default behaviour of any java EE spec.
In you case, you can define the mapping using annotations for the new database, then define a persistence.xml by declaring only the JNDI location and the flag:
<exclude-unlisted-classes>false</exclude-unlisted-classes>
Any other mapping will be handled on annotation level.
For the old database, you can declare another persistence unit and override the behaviour declared in the annotations by using persistence.xml or orm.xml.
In your code, you can refer to the appropriate persistence unit by name.

Categories

Resources