Is there a jakarta equivalent to javax.sql.DataSource and javax.persistence.EntityManagerFactory

We just upgraded to Spring 3.0.1, and replaced javax.peristence-api with jakarta.persistence-api.

REMOVED:

<dependency> <groupId>javax.persistence</groupId> <artifactId>javax.persistence-api</artifactId> <version>${javax.persistence.version}</version>
</dependency>

ADDED:

<dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>${jakarta.persistence.version}</version>
</dependency>

Question is this: Is there a jakarta replacement for these?

javax.sql.DataSource

javax.persistence.EntityManagerFactory

Not finding that the jakarta.persistence-api has a replacement for them

The error comes from this class. Might be legacy and an old way of managing JPA, transactions, datasource, etc... Might we be able to get rid of it, or update it? Hoping to find some helpful suggestions

package com.orders.persistence.jpa;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.util.Properties;
@Configuration
@EnableJpaRepositories( basePackages = "com.orders.persistence.jpa.repository", entityManagerFactoryRef = "entityManager", transactionManagerRef = "transactionManager")
@EnableTransactionManagement
public class PersistenceJPAConfig { @Value( "${driverClassName}" ) private String driverClassName; @Value( "${url}" ) private String url; @Value("${username}") private String jdbcUsername; @Value("${password}") private String jdbcPassword; @Value( "${hibernateDialect}" ) private String hibernateDialect; @Value( "${hibernateShowSql}" ) boolean hibernateShowSql; @Value( "${hibernateHbm2ddlAuto}" ) private String hibernateHbm2ddlAuto; @Value( "${jpaGenerateDdl}" ) boolean jpaGenerateDdl; @Value("${useQueryCache}") boolean useQueryCache; public PersistenceJPAConfig(){ super(); } @Bean public HibernateExceptionTranslator hibernateExceptionTranslator(){ return new HibernateExceptionTranslator(); } @Bean @Primary public LocalContainerEntityManagerFactoryBean entityManager(){ final LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean(); entityManager.setDataSource( dataSource() ); entityManager.setPackagesToScan(new String[] { "com.orders.persistence.jpa", "com.orders.persistence.model", "com.orders.persistence.service", }); final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(){ { setDatabase( Database.MYSQL ); setDatabasePlatform( hibernateDialect ); setShowSql( hibernateShowSql ); setGenerateDdl( jpaGenerateDdl ); } }; entityManager.setJpaVendorAdapter( vendorAdapter ); entityManager.setJpaProperties( additionalProperties() ); return entityManager; } @Bean @Primary public DataSource dataSource(){ final DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName( driverClassName ); dataSource.setUrl( url ); dataSource.setUsername( jdbcUsername ); dataSource.setPassword( jdbcPassword ); return dataSource; } @Bean @Primary public JpaTransactionManager transactionManager(){ final JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory( entityManager().getObject() ); return transactionManager; } @Bean public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(){ return new PersistenceExceptionTranslationPostProcessor(); } final Properties additionalProperties(){ return new Properties(){ {// use this to inject additional properties in the EntityManager setProperty("useSecondLevelCache", "org.hibernate.cache.RegionFactory"); setProperty("useQueryCache", "org.hibernate.cache.RegionFactory"); } }; }
}
2

3 Answers

I was stuck with this to, but some javax import are unchanged like javax.sql.dataSource.

These are not affected by the Jakarta EE namespace migration, but stay as they are. Some examples are:

javax.sql.* javax.transaction.xa.* javax.xml.* javax.naming.* javax.swing.*

1

just keep it, in my DB config file, it is:

import javax.sql.DataSource;
import jakarta.persistence.EntityManagerFactory;

I found a doc about this:

Unaffected (Non-EE) Packages

javax.sql is one of these packages, so you can use is AS-IS

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like