JPA之EntityManager踩坑及解决:更改PersistenceContext

网友投稿 766 2022-08-25


JPA之EntityManager踩坑及解决:更改PersistenceContext

目录JPAEntityManager踩坑之更改PersistenceContext一、原因二、SpringJPA多数据源怎么使用EntityManager三、使用EntityManager@PersistenceContext(unitName="entityManagerFactory")

JPA EntityManager踩坑之更改PersistenceContext

一、原因

项目中配置两个Spring JPA的数据源,使用EntityManager的时候默认是选择第一个,导致查询不到第二个数据源的数据。

二、Spring JPA多数据源怎么使用EntityManager

1. Spring JPA多数据源的配置请查看此文章:SpringData JPA基本/高级/多数据源使用

2. 引入EntityManager:

@PersistenceContext(unitName = "primaryPersistenceUnit")

private EntityManager entityManager;

@PersistenceContext中的unitName指的是使用哪一个数据源的persistenceUnit,persistenceUnit是在配置数据源的时候配置的,每一个数据源的persistenceUnit都是不一样的,这样就可以使用不同的数据源来查询数据了。

配置persistenceUnit:

@Bean(name = "entityManagerFactoryPrimary")

@Primary

public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {

return builder

.dataSource(primaryDataSource)

.properties(vendorProperties)

.packages("com.neo.model") //设置实体类所在位置

.persistenceUnit("primaryPersistenceUnit")

.build();

}

三、使用EntityManager

String SQL = "select id ,code,name,'desc' from drgo_cd10_dx_code where name like ('%"+content.trim()+"%') "

loggehttp://r.info("search getProductOwnerId SQL:"+SQL);

Query query = entityManager.createNativeQuery(SQL);

List dbList = query.getResultList();

List resultList = new ArrayList();

if (dbList!=null && dbList.size()>0){

for (int i =0;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 extends BaseDaoJpa

{

protected MyBaseDao()

{ }

//unitName与LocalContainerEntityManagerFactoryBean类的容器对象的名称一致

@PersistenceContext(unitName = "entityManagerFactory")

public void setJpaEntityManager(EntityManager entityManager)

{

super.setEntityManager(entityManager);

}

}

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 extends BaseDaoJpa

{

protected MyBaseDao()

{ }

//unitName与LocalContainerEntityManagerFactoryBean类的容器对象的名称一致

@PersistenceContext(unitName = "entityManagerFactory")

public void setJpaEntityManager(EntityManager entityManager)

{

super.setEntityManager(entityManager);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:python-- re 模块(python编程)
下一篇:python-- time 和 datetime(python爬虫)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~