Spring JPA没有连接到多个数据库:说实体经理找到2

时间:2017-08-18 14:05:45

标签: mysql spring jpa

除了现有的Persistenceconfig.Java之外,这是我添加的新文件。我找到了这个实体经理。

  1. 除了spring-data.xml中的jpa存储库之外,我们没有使用任何xml配置

  2. 问题仅出现在为记录活动新创建的一个包中,并且也包含在jpa存储库中。

  3. 在添加以下课程之前,以前的一切正常

    package com.jumbotree.kumcha.config;
    
    import java.util.Properties;
    
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.PersistenceContext;
    import javax.sql.DataSource;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.FactoryBean;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
    import org.springframework.dao.support.PersistenceExceptionTranslator;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    import org.springframework.jdbc.datasource.DriverManagerDataSource;
    //import org.springframework.orm.hibernate5.HibernateExceptionTranslator;
    import org.springframework.orm.hibernate4.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.HibernateJpaVendorAdapter;
    import org.springframework.transaction.PlatformTransactionManager;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    
    @Configuration
    @EnableTransactionManagement
    //@PropertySource("classpath:kumcha.properties")
    @PropertySource("file:/opt/jumbotree/kumcha2/kms.properties")
    @EnableJpaRepositories(basePackages = "com.jumbotree.kumcha.crm.model", entityManagerFactoryRef = "createEntityManagerFactoryChargingBean", transactionManagerRef = "createChargingTransactionManagerBean")
    @ImportResource("classpath:spring-data.xml")
    public class ChargingPersistenceConfig {
        @Autowired
        private Environment env;
    
        private static final Logger LOGGER = LoggerFactory.getLogger(ChargingPersistenceConfig.class);
    
    
        @Bean  
        public DataSource createChargingDataSourceBean() {
            LOGGER.info("Charging Datasource created...");
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName(env.getProperty("charging.db.driver"));
            dataSource.setUrl(env.getProperty("charging.db.url"));
            dataSource.setUsername(env.getProperty("charging.db.username"));
            dataSource.setPassword(env.getProperty("charging.db.password"));
            return dataSource;
        }
    
        //Here is the entity manager added and causing this issue
        @Bean   //(name = "entityManagerFactoryCharging")
        @PersistenceContext (unitName="chargingPU")
        public FactoryBean<EntityManagerFactory> createChargingEntityManagerFactoryBean(@Qualifier("createChargingDataSourceBean") DataSource dsc) {
            LocalContainerEntityManagerFactoryBean chargingentityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            LOGGER.info("Charging entityman created...");
            try {
                chargingentityManagerFactoryBean.setDataSource(createChargingDataSourceBean());
                chargingentityManagerFactoryBean.setPackagesToScan("com.jumbotree.kumcha.crm.model");
                chargingentityManagerFactoryBean.setJpaVendorAdapter(createJpaVendorAdapterBean());
                chargingentityManagerFactoryBean.setJpaProperties(createJpaProperties());
            } catch (Exception e) {
                // TODO: handle exception
                LOGGER.error(e.toString());
            }
    
    
            return chargingentityManagerFactoryBean;
        }
    
        private Properties createJpaProperties() {
            LOGGER.info("hibernate.show_sql :::: "+env.getProperty("hibernate.show_sql"));
            return new Properties() {
                {
    //                setProperty("hibernate.hbm2ddl.auto", "create-drop");
                    setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
                    setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
                   // setProperty("hibernate.cache.use_second_level_cache", "true");
                   // setProperty("hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider");
                   // setProperty("shared-cache-mode", "DISABLE_SELECTIVE");
                    //<property name="hibernate.cache.use_second_level_cache" value="true"/>
                    //setProperty("hibernate.connection.zeroDateTimeBehavior", "convertToNull");
                }
            };
        }
    
        private JpaVendorAdapter createJpaVendorAdapterBean() {
            HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    //        jpaVendorAdapter.setDatabase(Database.valueOf(env.getProperty("db.name")));
            jpaVendorAdapter.setShowSql(true);
    //        jpaVendorAdapter.setGenerateDdl(true);
            jpaVendorAdapter.setDatabasePlatform(env.getProperty("hibernate.dialect"));
            return jpaVendorAdapter;
        }
    
        @Bean   //(name = "transactionManager")
        public PlatformTransactionManager createChargingTransactionManagerBean() throws Exception {
            LOGGER.info("Charging transactionMan created...");
            JpaTransactionManager chargingtransactionManager = new JpaTransactionManager();
            chargingtransactionManager.setEntityManagerFactory(createChargingEntityManagerFactoryBean(createChargingDataSourceBean()).getObject());
            return chargingtransactionManager;
        }
    
        @Bean
        public PersistenceExceptionTranslationPostProcessor createPersistenceExceptionTranslationPostProcessor() {
            return new PersistenceExceptionTranslationPostProcessor();
        }
    
        // Required if using Hibernate 4
        @Bean
        public PersistenceExceptionTranslator createPersistenceExceptionTranslatorBeaan() {
            return new HibernateExceptionTranslator();
        }
    
    }
    

1 个答案:

答案 0 :(得分:3)

LoggerRepository无法选择其中一个createChargingEntityManagerFactoryBeancreateEntityManagerFactoryBean

将其中一个设为主要和/或指定限定符。 (顺便说一句,即使有限定词,也必须使其中一个豆成为主要的)

如果没有您的代码,则无法建议更改cconfig。