我们刚刚升级到Spring3.0.1,并将javax.peristence-api替换为jakarta.persistence-api。
删除:
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>${javax.persistence.version}</version>
</dependency>
添加:
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>${jakarta.persistence.version}</version>
</dependency>
问题是雅加达有替代品吗?
javax.sql.DataSource
javax.persistence.EntityManagerFactory
没有发现jakarta.persistence-api有它们的替代品
错误来自这个类。可能是管理JPA、事务、数据源等的旧方法。我们可以摆脱它,或者更新它吗?希望能找到一些有用的建议
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条答案
按热度按时间cnh2zyt31#
我一直坚持这样做,但是有些javax导入没有改变,比如javax.sql.dataSource。
这些不受Jakarta EE命名空间迁移的影响,但保持原样。一些例子是:
javax.sql. * javax.transaction.xa.* javax.xml.* javax.naming.* javax.swing.*
https://www.agilejava.eu/2021/01/22/migration-guide/
bvjveswy2#
只要保留它,在我的DB配置文件中,它是:
导入javax.sql.DataSource;
import jakarta.persistence.EntityManagerFactory;