I upgraded Java from 1.8 to 17 , Spring Boot from 2.1.18.RELEASE to 3.1.1 and Postgresql from 42.2.16.jre7 to 42.6.0
I was using this in my app prop: spring.jpa.database-platform = org.hibernate.dialect.PostgreSQL94Dialect
I changed it with this: spring.jpa.database-platform = org.hibernate.dialect.PostgreSQLDialect
I am using multiple database connections; primary and second. The code below was working fine before the upgrade.
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import jakarta.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.sql.DriverManager;
import java.sql.SQLException;
@Configuration
@EnableJpaAuditing
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "primaryEntityManagerFactory",
transactionManagerRef = "primaryTransactionManager",
basePackages = {"xxxxx"})
public class PrimaryDatabaseConfig {
@Primary
@Bean(name = "primaryDataSourceConfig")
@ConfigurationProperties("spring.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "primaryDataSource")
public DataSource dataSource(@Qualifier("primaryDataSourceConfig") DataSourceProperties dataSourceProperties) throws SQLException {
String url = dataSourceProperties.determineUrl();
String user = dataSourceProperties.determineUsername();
String password = dataSourceProperties.determinePassword();
return new SimpleDriverDataSource(DriverManager.getDriver(url), url, user, password);
}
@Primary
@Bean(name = "primaryEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean primaryEntityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("primaryDataSource") DataSource dataSource) {
return builder.dataSource(dataSource)
.packages("xxxxx")
.persistenceUnit("primaryPU")
.build();
}
@Primary
@Bean(name = "primaryTransactionManager")
public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
return new JpaTransactionManager(entityManagerFactory);
}
}
I was importing EntityManagerFactory from javax.
"import javax.persistence.EntityManagerFactory;"
But with JDK17 I read that jakarta should be used instead of javax and in my project i replaced javax with jakarta. It works fine in other places I changed. But after the change here I am getting the following error.
What I've tried:
I downgraded the Postgres version and reverted the changes. It caused conflict.
- I added the following dependency to my pom file:
\<dependency\>
\<groupId\>javax.persistence\</groupId\>
\<artifactId\>javax.persistence-api\</artifactId\>
\<version\>2.2\</version\>
\</dependency\>
But it need to jakarta, not javax.
public JpaTransactionManager( jakarta.persistence.EntityManagerFactory emf )
- I changed my application.properties from this :
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl spring.jpa.properties.hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy spring.jpa.properties.hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
to this:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy spring.jpa.hibernate.naming.implicit-strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
I couldn't find any other solution about this error.
How can i fix it?
javax.persistencelingering around somewhere and make sure you have changed all dependencies. Also Java17 and Jakarta have nothing to do Spring Boot 3 and Spring 6 are for JakartaEE and not JavaEE so that is the driving force for these changes. Finally do you really get an exception while running or are you just scred by the warning in the IDE? I suspect the latter.