Spring - Issue setting dialect in Hibernate JPA Configuration - java

I'm moving from a Spring Boot 1.5.9 to a normal Spring project.
In application.properties I've added
spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.autoconfigure.exclude[2]=org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration
spring.autoconfigure.exclude[3]=org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
In application.properties I have a line that defines the dialect I'm using, it worked properly with the autoconfiguration
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
When I try to run the project with the new configuration class I have org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory', I have the same error with the autoconfiguration if i remove the dialect line.
My way to specify the properties, including the dialect, in the configuration class is the following
private Map<String, String> properties = new HashMap<String, String>();
public HibernateJpaConfig() {
properties.put("hibernate.hbm2ddl.auto", "create-drop");
properties.put("hibernate.ejb.naming_strategy", "org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl");
}
I noticed a warning message that might be my issue
WARN 13292 --- [ restartedMain] org.hibernate.orm.deprecation : HHH90000006: Attempted to specify unsupported NamingStrategy via setting [hibernate.ejb.naming_strategy]; NamingStrategy has been removed in favor of the split ImplicitNamingStrategy and PhysicalNamingStrategy; use [hibernate.implicit_naming_strategy] or [hibernate.physical_naming_strategy], respectively, instead.
If I try to replace the second value of the map with one of those I still have the same error.

Please look at this release note of spring boot.
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.4-Release-Notes#naming-strategy
I think problem is in naming_strategy.
SpringNamingStrategy is no longer used as Hibernate 5.1 has removed support for the old NamingStrategy interface. A new SpringPhysicalNamingStrategy is now auto-configured which is used in combination with Hibernate’s default ImplicitNamingStrategy. This should be very close to (if not identical) to Spring Boot 1.3 defaults, however, you should check your Database schema is correct when upgrading.
If you were already using Hibernate 5 before upgrading, you may be using Hibernate’s 5 default. If you want to restore them after the upgrade, set this property in your configuration:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Related

Spring Boot doesn't override default property by profile property in Map

I have a property with an array value that I want to override when a certain profile is active. But instead of replacing the property Spring Boot performs a strange merge of two arrays. It worked fine on Spring Boot version 1.5 but stopped after upgrading to version 2.5.
I have an application.yml that looks like this:
spring.config.use-legacy-processing: true
configuration:
type:
my-type:
... # some configuration properties
my-list: ['first','second','third']
---
spring:
profiles: dev
configuration:
type:
my-type:
... # some configuration properties
my-list: ['fourth']
I am retrieving this configuration as Map and for property my-list when running with dev profile I am getting an array ['fourth','second','third']. It is the default array with the first element replaced by the value from the profile configuration. If I would replace profile array with ['fourth','fifth'] then the result would be ['fourth','fifth','third'].
I tried to disable legacy processing and use spring.active.on-profile it still works the same way.
If I am consuming properties to class instead of Map then everything works fine but the list of properties is not fixed.
Maybe someone knows what could be done about this?

Property in IntelliJ: driver-class-name or driverClassName?

I use IntelliJ IDEA, the IntelliSense suggests the usage of assigning jdbc driver in application.properties file as
spring.datasource.driver-class-name=com.microsoft.jdbc.sqlserver.SQLServerDriver
See the image when I typing
But by the answer, that is wrong. It should be
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
However a web site related to JetBrains seems indicate that using driver-class-name might be correct.
https://youtrack.jetbrains.com/issue/IDEA-202820?_ga=2.207495315.1822682194.1613252382-718343134.1609267918
So I am confused. Which one is right? Is it a bug of IntelliJ IDEA?
Spring Boot supports both formats for properties, and they can be used interchangeably for properties defined by property binding (#ConfigurationProperties beans).
See also Relaxed Binding in the Spring Boot Features documentation:
Spring Boot uses some relaxed rules for binding Environment
properties to #ConfigurationProperties beans, so there does not need
to be an exact match between the Environment property name and the
bean property name. Common examples where this is useful include
dash-separated environment properties (for example, context-path
binds to contextPath), and capitalized environment properties (for
example, PORT binds to port).
In other words, given spring.datasource.driverClassName is defined through a #ConfigurationProperties bean, you can use both spring.datasource.driver-class-name and spring.datasource.driverClassName. The kebab-case form is the recommended form. The relaxed binding was - AFAIK - introduced in Spring Boot 2, so maybe the question you referenced was still at Spring Boot 1.x.
In any case, IntelliJ's autocomplete cannot be wrong in this case, because it uses information contained in the Spring Boot JAR files, generated by Spring Boot tools, specifically for spring.datasource.driver-class-name, this property name is obtained from META-INF/additional-spring-configuration-metadata.json in the spring-boot-autoconfigure JAR file. It is also the property listed in Common Application properties.
The Youtrack issue you listed seems to be an unrelated problem.

spring.datasource.initialize is deprecated

I have a springboot application where I am trying to add following to application.properties file
spring.datasource.initialize=false
When I add this I see a warning as below:
I tried finding out what's the new property that replaces this deprecated property but in vain.
Can anybody help on this!
Having a reference to a migration guide would be great.
In Spring Boot 2.5, 'spring.datasource.initialization-mode' has been depracated as well:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.5-Release-Notes#SQL-Script-DataSource-Initialization
you should use:
spring.sql.init.mode=always
or
spring.sql.init.mode=never
You can read more at:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto.data-initialization
As per the document
Spring Boot automatically creates the schema of an embedded
DataSource. This behaviour can be customized by using the
spring.datasource.initialization-mode property. For instance, if you
want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
Look at this migration guide
As per Spring Boot Migration mentioned in Github
Basic DataSource initialization is now only enabled for embedded data
sources and will switch off as soon as you’re using a production
database. The new spring.datasource.initialization-mode (replacing
spring.datasource.initialize) offers more control.
spring.datasource.initialization-mode=always
The property spring.datasource.initialization-mode from Spring boot verion 2.7 and onwards is not any more depracated. It has been completely removed!
So the change into the replacement property spring.sql.init.mode is a must do from now on.
Spring Boot 2.7 changelog
You can use spring.jpa.defer-datasource-initialization. Refer to this Spring Documentation on how to Initialize a Database Using Basic SQL Scripts:
spring.jpa.defer-datasource-initialization=true
spring.sql.init.enabled=true - to initialize database by data.sql script located in application resources
spring.sql.init.enabled=false - to

Disable Redis AutoConfig in spring boot when testing

I am trying to disable Redis when I am testing with spring boot. I have disabled my configuration but the auto config created a default connection and fails because it can't connect to a non-existent service. For testing I am content to just use a basic in-memory cache or a no-op cache. That doesn't work either. Here is what I have tried:
per this issue I added said configuration to my test app properties
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
But. That gets me a bit further. But ultimately I get a NoSuchBeanDefinitionException redisTemplate - this is because redisReferenceResolver is trying to look that up.
Looking at my debugger right now, the bean it's trying to hydrate is:
org.springframework.data.redis.core.convert.ReferenceResolverImpl which is coming from spring-data-redis:1.8.0.RELEASE which is coming from this dependency: compile('org.springframework.boot:spring-boot-starter-data-redis') . I admit, the bean name is a bit misleading. The type it actually resolves to is not
The only other reference to redis is in our hibernate support.
Can someone explain how to turn this off for testing?
Try excluding this two auto-configuration classes in your test properties file:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
or
exclude
org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
and set: spring.data.redis.repositories.enabled=false
With YAML syntax (& Spring Boot):
spring.autoconfigure:
exclude:
- org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
- org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
If you have SystemEnvironmentPropertySource in you app context you can use environment variable SPRING_AUTOCONFIGURE_EXCLUDE separating items with comma:
SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration
Also try #EnableAutoConfiguration(exclude = {...}) on a #TestConfiguration annotated class.
If you dont want to change any files/code, you can also do this with an environment variable:
SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration

SpringBoot 1.3.0 support hibernate 5?

I'm a little confused about SpringBoot's (1.3.0) support of Hibernate5. The reference lists a dependency on hibernate 4.3.11.Final but it also lists a dependency on SpringFramework 4.2.3 which includes Hibernate5 support.
Is it just a matter of adding the extra Hibernate5 dependencies to override what Boot bundles? Can someone please clarify for me?
You can use either Hibernate 4.3 or Hibernate 5.0 with Spring Boot 1.3. As you've observed, Hibernate 4.3.x is the default version.
To use Hibernate 5.0 you should override the hibernate.version property in Spring Boot's dependency management. Assuming that you're using Maven:
<properties>
<hibernate.version>5.0.5.Final</hibernate.version>
</properties>
When using Hibernate 5.0, the one big difference from using Hibernate 4.3.x is that you'll lose Spring Boot's custom naming strategy. Due to a breaking change made in Hibernate 5.0, you'll see a warning like this logged at startup:
2015-12-07 10:04:56.911 WARN 81371 --- [ main] org.hibernate.orm.deprecation : HHH90000006: Attempted to specify unsupported NamingStrategy via setting [hibernate.ejb.naming_strategy]; NamingStrategy has been removed in favor of the split ImplicitNamingStrategy and PhysicalNamingStrategy; use [hibernate.implicit_naming_strategy] or [hibernate.physical_naming_strategy], respectively, instead.
If you dislike Hibernate 5's defaults, you can specify a custom implicit or physical naming strategy in Spring Boot's application.properties using the spring.jpa.properties.hibernate.implicit_naming_strategy and spring.jpa.properties.hibernate.physical_naming_strategy properties respectively.
Update July 2016: With the release of Spring Boot 1.4.0 the default Hibernate 5 is used as the default JPA persistence provider.
There is a ticket about migrating to Hibernate 5 for some time now - it seems the main setback is some name strategy incompatibility. Asof now, the ticket is currently scheduled for 1.4.0
Thanks guys! after many trials, this solution worked for me like a charm! I implemented custom strategy and set them in application.yml as shown below:
jpa:
database: MYSQL
database-platform: org.hibernate.dialect.MySQL5Dialect
properties:
hibernate:
implicit_naming_strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
physical_naming_strategy: com.quicken.ups.entities.utils.DBFieldNamingStrategy

Categories

Resources