Spring JPA No qualifying bean of type: javax.persistence.EntityManagerFactory

You can define two EntityManagers in one class:
1. Provide a name for your EntityManager Beans
2. Provide a persistentUnitName and make sure it is unique.

As the entityManager injects by name into the transaction manager make sure you entitymanager bean name is same as your transaction manager parameter. In this case my entitytManager bean name is “dwEntityManager” and my transactionmanager has the same name: javax.persistence.EntityManagerFactory dwEntityManager

package com.sforce.jpa;

import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;

import org.slf4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import com.sforce.model.DWAccount;
import com.smallbusiness.model.DWBusiness;

@PropertySource("classpath:salesforcesync.properties")
@Configuration
@EnableTransactionManagement
public class JpaConfigurationImpl {
	
	 private static final Logger logger = org.slf4j.LoggerFactory.getLogger(JpaConfigurationImpl.class);
	
	 @Inject @Named("lcDwDataSource") DataSource lcDwDataSource;
	 @Inject @Named("lcDataSource") DataSource lcDataSource;
	 
	 @Inject private Environment environment;
	
	 @Bean(name="dwEntityManager")
	 public LocalContainerEntityManagerFactoryBean dwEntityManagerFactory() {
		 try {
			  LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
		      localContainerEntityManagerFactoryBean.setDataSource(lcDwDataSource);
		      localContainerEntityManagerFactoryBean.setPackagesToScan(DWAccount.class.getPackage().getName());
		      localContainerEntityManagerFactoryBean.setPersistenceUnitName("dw");
		      HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
		      
		      jpaVendorAdapter.setGenerateDdl(false);
		      jpaVendorAdapter.setShowSql(true);
		      jpaVendorAdapter.setDatabasePlatform(environment.getProperty("dataSource.dialect"));
		      localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
		      return localContainerEntityManagerFactoryBean;
		      
		 } catch (Exception e) {
			logger.error("JpaConfigurationImpl.entityManagerFactory(): " + e.getMessage());
		 }
		 return new LocalContainerEntityManagerFactoryBean();
	 }
	 
	 @Bean(name="dwTransactionManager")
	 public PlatformTransactionManager dwTransactionManager(javax.persistence.EntityManagerFactory dwEntityManager) {
		try {
			return new JpaTransactionManager(dwEntityManager);
		} catch (Exception e) {
			logger.error("JpaConfigurationImpl.transactionManager(): " + e.getMessage());
		}
		return new JpaTransactionManager();
	 }
	 
	 @Bean(name="lcEntityManager")
	 public LocalContainerEntityManagerFactoryBean sbEntityManagerFactory() {
		 try {
			  LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
		      localContainerEntityManagerFactoryBean.setDataSource(lcDataSource);
		      localContainerEntityManagerFactoryBean.setPackagesToScan(DWBusiness.class.getPackage().getName());
		      localContainerEntityManagerFactoryBean.setPersistenceUnitName("lc");
		      HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
		      
		      jpaVendorAdapter.setGenerateDdl(false);
		      jpaVendorAdapter.setShowSql(true);
		      jpaVendorAdapter.setDatabasePlatform(environment.getProperty("dataSource.dialect"));
		      localContainerEntityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
		      return localContainerEntityManagerFactoryBean;
		      
		 } catch (Exception e) {
			logger.error("JpaConfigurationImpl.entityManagerFactory(): " + e.getMessage());
		 }
		 return new LocalContainerEntityManagerFactoryBean();
	 }
	 
	 @Bean(name="lcTransactionManager")
	 public PlatformTransactionManager sbTransactionManager(javax.persistence.EntityManagerFactory lcEntityManager) {
		try {
			return new JpaTransactionManager(lcEntityManager);
		} catch (Exception e) {
			logger.error("JpaConfigurationImpl.transactionManager(): " + e.getMessage());
		}
		return new JpaTransactionManager();
	 }
}

Now that we have the two entityManagers defined we can call them respectively by specifying the unitName for each entityManager:

@PersistenceContext(unitName="dw")
private EntityManager dwEntityManager;

@PersistenceContext(unitName="lc")
private EntityManager lcEntityManager;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s