JPA之EntityManager踩坑及解決:更改PersistenceContext
JPA EntityManager踩坑之更改PersistenceContext
一、原因
項目中配置兩個Spring JPA的數(shù)據(jù)源,使用EntityManager的時候默認(rèn)是選擇第一個,導(dǎo)致查詢不到第二個數(shù)據(jù)源的數(shù)據(jù)。
二、Spring JPA多數(shù)據(jù)源怎么使用EntityManager
1. Spring JPA多數(shù)據(jù)源的配置請查看此文章:SpringData JPA基本/高級/多數(shù)據(jù)源使用
2. 引入EntityManager:
@PersistenceContext(unitName = "primaryPersistenceUnit") private EntityManager entityManager;
@PersistenceContext中的unitName指的是使用哪一個數(shù)據(jù)源的persistenceUnit,persistenceUnit是在配置數(shù)據(jù)源的時候配置的,每一個數(shù)據(jù)源的persistenceUnit都是不一樣的,這樣就可以使用不同的數(shù)據(jù)源來查詢數(shù)據(jù)了。
配置persistenceUnit:
@Bean(name = "entityManagerFactoryPrimary")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
? ? return builder
? ? ? ? ? ? .dataSource(primaryDataSource)
? ? ? ? ? ? .properties(vendorProperties)
? ? ? ? ? ? .packages("com.neo.model") //設(shè)置實體類所在位置
? ? ? ? ? ? .persistenceUnit("primaryPersistenceUnit")
? ? ? ? ? ? .build();
}三、使用EntityManager
String SQL = "select id ,code,name,'desc' from drgo_cd10_dx_code where name like ('%"+content.trim()+"%') "?
logger.info("search getProductOwnerId SQL:"+SQL);
Query query = entityManager.createNativeQuery(SQL);
List<Object[]> dbList = query.getResultList();
List<ICD10CodeData> resultList = new ArrayList<ICD10CodeData>();
if (dbList!=null && dbList.size()>0){
??? ?for (int i =0;i<dbList.size();i++){
? ? ? Object[] o = dbList.get(i);
? ? ? String id = o[0]==null?"":o[0].toString();
? ? ? String code = o[1]==null?"":o[1].toString();
? ? ? String name = o[0]==null?"":o[2].toString();
? ? ? String desc = o[1]==null?"":o[3].toString();
? ? ? ICD10CodeData tempCode = new ICD10CodeData(Long.parseLong(id), code, name, desc);
?? ? ?resultList.add(tempCode);
? ? ?}
?}@PersistenceContext(unitName = "entityManagerFactory")
@Configuration
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
@ComponentScan({cn.com.appName.manager,cn.com.appName.dao,cn.com.appName.dao.jpa})
public class AppConfig extends AbstractManager
{
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean()
{
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource(null));
factoryBean.setPackagesToScan(new String[] { "cn.com." + AppConfig.APP_NAME + ".entity" });
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabasePlatform(hibernateDialect);
if (logger.isDebugEnabled())
{
// vendorAdapter.setShowSql(true);
// vendorAdapter.setGenerateDdl(true);
}
//vendorAdapter.setGenerateDdl(true);
factoryBean.setJpaVendorAdapter(vendorAdapter);
// factoryBean.setJpaProperties(this.additionlProperties());
return factoryBean;
}
}
public abstract class MyBaseDao<E extends BaseEntity> extends BaseDaoJpa<E>
{
protected MyBaseDao()
{ }
//unitName與LocalContainerEntityManagerFactoryBean類的容器對象的名稱一致
@PersistenceContext(unitName = "entityManagerFactory")
public void setJpaEntityManager(EntityManager entityManager)
{
super.setEntityManager(entityManager);
}
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java使用@Scheduled注解執(zhí)行定時任務(wù)
這篇文章主要給大家介紹了關(guān)于java使用@Scheduled注解執(zhí)行定時任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
java?LeetCode刷題稍有難度的貪心構(gòu)造算法
這篇文章主要為大家介紹了java?LeetCode刷題稍有難度的貪心構(gòu)造題解示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實現(xiàn)
本文主要介紹了Spring boot基于JPA訪問MySQL數(shù)據(jù)庫的實現(xiàn),Spring boot結(jié)合Jpa 能夠簡化創(chuàng)建 JPA 數(shù)據(jù)訪問層和跨存儲的持久層功能,用戶的持久層Dao接口只需要繼承定義好的接口,感興趣的可以了解一下2021-06-06
spring boot加入攔截器Interceptor過程解析
這篇文章主要介紹了spring boot加入攔截器Interceptor過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10
spring cloud gateway如何獲取請求的真實地址
這篇文章主要介紹了spring cloud gateway如何獲取請求的真實地址問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot 普通類調(diào)用Bean對象的一種方式推薦
這篇文章主要介紹了SpringBoot 普通類調(diào)用Bean對象的一種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java實戰(zhàn)之圖書管理系統(tǒng)的實現(xiàn)
這篇文章主要介紹了如何利用Java語言編寫一個圖書管理系統(tǒng),文中采用的技術(shù)有Springboot、SpringMVC、MyBatis、ThymeLeaf 等,需要的可以參考一下2022-03-03

