Disable Envers in properties file - spring-boot

I have already referred to this question: Spring Boot 2 - disable Envers, but the answer did not work for me.
I am using Spring Boot and would like to disable Envers in the properties file without having to go into my code and remove any #Audited annotations or such.
I have tried the following to no avail:
hibernate.integration.envers.enable=false
spring.jpa.properties.hibernate.integration.envers.enable=false
spring.jpa.properties.org.hibernate.integration.envers.enable=false
hibernate.integration.envers.enabled=false
spring.jpa.properties.hibernate.integration.envers.enabled=false
spring.jpa.properties.org.hibernate.integration.envers.enabled=false
hibernate.envers.autoRegisterListeners=false
spring.jpa.properties.org.hibernate.envers.autoRegisterListeners=false

I cannot speak specifically to the integration with Spring Boot, but you should be able to force it to be disabled by supplying a custom hibernate.properties file shown below:
# this disables hibernate envers, even if its on the classpath
hibernate.integration.envers.enabled=false

Related

Externalizing configuration for Hibernate Search

I am running hibernate search with spring boot. I have written a working configuration for my application. How ever, i want to externalize my configuration and use ./config/hibernate.properties instead of src/main/resources/hibernate.properties. After copying my properties file to the desired location, i am getting and exception:
nested exception is java.io.FileNotFoundException: class path resource [hibernate.properties] cannot be opened because it does not exist
Anyone with any idea on how i should tell spring to read my configuration file?
Move your configuration to an src/main/resources/application.properties file and prepend spring.jpa.properties. everywhere, so hibernate.dialect will become spring.jpa.properties.hibernate.dialect, for example.
Then you can use Spring features to move your configuration wherever you want. To move it to ./config/application.properties I suppose you will have to add #PropertySource("./config/application.properties") to one of your #Configuration classes, or something similar.
I'm sure you can also keep the hibernate configuration in a separate file (separate from the rest of your application configuration).
See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for more details about externalizing configuration in Spring Boot.
For some reason, it seems hibernate-search will prevent application from starting as long as a hibernate.properties configuration file does not exist. After trying for a while without success, i found a work around for my problem.
First, i created an empty hibernate.properties file and place it under src/main/resources.
Secondly, i moved all hibernate-search configurations to application.properties as follows:
spring.jpa.properties.hibernate.search.default.indexmanager = elasticsearch
spring.jpa.properties.hibernate.search.default.elasticsearch.host = http://my-server.com
spring.jpa.properties.hibernate.search.default.elasticsearch.index_schema_management_strategy = CREATE
spring.jpa.properties.hibernate.search.default.elasticsearch.required_index_status = yellow
This way, the application will start and spring will get all configuration from the externalized configuration as documented here.

Spring Boot 2 - disable Envers

How can I disable Hibernate Envers in Spring Boot 2? I don't want to remove the dependency, because the code relies on it depending on the environment.
I tried the following properties in my application.properties but none worked. On startup it always prints envers enabled = true
spring.hibernate.integration.envers.enabled=false
hibernate.integration.envers.enabled=false
spring.jpa.hibernate.integration.envers.enabled=false
Can you try;
spring.jpa.properties.hibernate.integration.envers.enabled=false
"Non-Spring Data JPA" Hibernate properties are configured through spring.jpa.properties.hibernate.* afaik. That might just do it!
Actually there are two ways to disable hibernate envers in spring boot applications:
Wih VM Arguments:
-Dhibernate.integration.envers.enabled= false
With hibernate.properties (not application.properties) file, (if not exist, create) add the line below.
hibernate.integration.envers.enabled=false

Importing Data with spring boot

I have flyway and spring-boot working correctly, but I can't seem to wire up my spring.datasource.data correctly.
If I have a file src/main/resources/db/seeds/one_project.sql. I have tried the following inside my application.properties file.
# fully qualified path
spring.datasource.data=file:///fully/qualified/path/db/seeds/one_project.sql
# classpath specific
spring.datasource.data=classpath:/db/seeds/one_project.sql
# relative path
spring.datasource.data=/db/seeds/one_project.sql
The only thing I can actually get to work is to copy one_project.sql to src/main/resources/schema.sql ( even copying it to src/main/resources/data.sql does not work.
Is there something I am completely missing from the documentation?
I have been following along the documentation here.
Thanks in advance for the help!
Stuck at that quite long.
My context: Spring Boot 2.2.6 + Hibernate 5.4 + script.sql in classpath(src/main/resources).
To make script executed at application start I was need to add in application.properties:
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=update
spring.datasource.data=classpath:script.sql
And remove all comments BEFORE the actual code and BETWEEN code in script.sql.
Or if you need comments, add SELECT 1; on the next line after the line with comment. Because the next line after commented one seems to be ignored. No matter how many line breaks after line with comment you paste.
As i can see it, Spring Boot executes the data scripts if one of the following conditions is true:
The schema.sql script is present and the initialization is enabled (spring.datasource.initialize=true)
If JPA and Hibernate is used and autoconfigured with Spring Boot: The property hibernate.hbm2ddl.auto is present (the value doesn't matter, you can give it an empty string or just "validate") and the initialization is enabled (spring.datasource.initialize=true).
TL;DR
Create a blank schema.sql if you want your data.sql to run.
Also as stated in a comment it must execute one line such as `select 1` or `select 1 from dual`
You said
The only thing I can actually get to work is to copy one_project.sql to src/main/resources/schema.sql
Which makes me think it's evident you don't have a schema.sql
So just create a blank schema.sql and then it will run one_project.sql
Source Code -https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializer.java
As you can see it gathers the schema locations and if schema resources are empty then it doesn't continue to run the data.sql (this is at the top of the runSchemaScripts() method)
The way I got it working was by using the following properties
spring.datasource.data=classpath:prod.sql
spring.datasource.url=jdbc:mysql://localhost:3306/DATABASENAME?useSSL=false
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD
spring.datasource.initialization-mode=always
spring.datasource.initialization-mode=always seems to do the trick
Try using classpath*, like the following:
spring.datasource.schema=classpath*:db/seeds/your_schema.sql
spring.datasource.data=classpath*:db/seeds/one_project.sql
As of Spring Boot version 2.7 and onwards the provided here solution of
spring.datasource.initialization-mode=always
spring.jpa.hibernate.ddl-auto=update
spring.datasource.data=classpath:script.sql
will stop working since the properties spring.datasource.data and spring.datasource.initialization-mode have been removed from spring boot. The replacements spring.sql.init.data-locations and spring.sql.init.mode should be used instead.
So the solution would be
spring.sql.init.mode=always
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.data-locations=classpath:script.sql
See relevant Spring Boot 2.7 changelog

How to deal with defaultRolePrefix="ROLE_" in Spring Security update from 3.2.7 to 4.0.2.RELEASE

My Spring Boot application works on Spring Security 3.2.7.RELEASE.
Now, I'd like to update it to 4.0.2.RELEASE.
After hours of debug I have found that Spring Security 4.0.2.RELEASE uses defaultRolePrefix="ROLE_"
in
org.springframework.security.access.expression.SecurityExpressionRoot.hasAnyAuthorityName(String prefix, String... roles) method
In my application I use roles without this prefix and accordingly I get AccessDeniedException.
How to configure Spring Boot in order to use SecurityExpressionRoot.defaultRolePrefix="" ?
I found the solution how to fix it. I need to change hasRole to hasAuthority, for example:
#PreAuthorize("hasAuthority('PERMISSION_CREATE_NODE')")
In the other hand you can remove role prefix ass described here. In this cas you are free to use other annotations.

Spring Boot, Hibernate Search properties

How to provide Hibernate Search parameters when using Spring Boot?
...
spring.datasource.driverClassName=org.postgresql.Driver
hibernate.search.jmx_enabled=true
hibernate.search.default.directory_provider=filesystem
hibernate.search.generate_statistics=true
hibernate.search.lucene_version=LUCENE_CURRENT
hibernate.search.default.indexBase=/mypath-to-index
It does not care what I provide. Default settings always get applied.
I think below code does not have anything to process properties related to Hibernate Search. Can that be the issue?
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java
You can put them in the application.properties file if you put "spring.jpa.properties." in front of the property names.
Example:
spring.jpa.properties.hibernate.search.jmx_enabled=true
spring.jpa.properties.hibernate.search.default.directory_provider=filesystem
spring.jpa.properties.hibernate.search.generate_statistics=true
spring.jpa.properties.hibernate.search.lucene_version=LUCENE_CURRENT
spring.jpa.properties.hibernate.search.default.indexBase=/mypath-to-index
Spring will take any properties under spring.jpa.properties.* and pass them along (with the prefix stripped) once the EntityManagerFactory is created.
Got it working.
Put another property file named "hibernate.properties" inside src/main/resources with below content.
hibernate.search.jmx_enabled=true
hibernate.search.default.directory_provider=filesystem
hibernate.search.generate_statistics=true
hibernate.search.lucene_version=LUCENE_CURRENT
hibernate.search.default.indexBase=/mypath-to-index

Resources