JpaRepositoryFactoryBean有错误的entitymanager

时间:2015-07-24 13:31:25

标签: spring datasource entitymanager

给定的entitymanager没有我的类的托管类型,它是错误的实体管理器。

我认为通过简单地定义模型和存储库扫描到特定的命名空间会自动处理这个问题,但因为我觉得我错了(?!)。有人知道如何处理这个问题吗?

他有类something.application.model.User但使用entitymanager和命名空间的托管类型something.manager.model。

public class JpaConfiguration {

@Configuration
@EntityScan(basePackages = { "something.application.model" })
@EnableJpaAuditing
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "something.application.model" }, repositoryFactoryBeanClass = RepositoryFactoryBean.class, entityManagerFactoryRef = "applicationEntityManagerFactory", transactionManagerRef = "applicationTransactionManager")
public static class ApplicationJpaConfiguration {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * The datasource used by the system. Configuration see
     * /src/main/resources/application.properties.
     */
    @Primary
    @Bean(name="applicationDataSource")
    @ConfigurationProperties(prefix = "spring.application.datasource")
    public DataSource applicationDataSource() {
        return new TenantAwareDataSource();
    }

    /**
     * The transaction manager used by the application system. 
     * 
     * @param javax.sql.DataSource applicationDataSource
     * @param javax.sql.DataSource loadTimeWeaver
     * @return org.springframework.orm.jpa.JpaTransactionManager
     */
    @Primary
    @Bean(name="applicationTransactionManager")
    public JpaTransactionManager applicationTransactionManager(LoadTimeWeaver loadTimeWeaver) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(applicationEntityManagerFactory(loadTimeWeaver).getObject());
        return transactionManager;
    }

    /**
     * The entity manager factory used by application system. Specific properties of the
     * persistence provider eclipselink are configured here.
     */
    @Primary
    @Bean(name="applicationEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean applicationEntityManagerFactory(LoadTimeWeaver loadTimeWeaver) {

        Map<String, String> jpaProperties = new HashMap<String, String>();
        jpaProperties.put("eclipselink.ddl-generation", "create-or-extend-tables");
        jpaProperties.put("eclipselink.session.customizer", HistoryBuildingSessionCustomizer.class.getName());
        jpaProperties.put("eclipselink.logging.level.sql", "fine");
        jpaProperties.put("eclipselink.logging.parameters", "true");
        jpaProperties.put("eclipselink.temporal.mutable", "true");
        jpaProperties.put("eclipselink.persistence-context.flush-mode", "commit");
        jpaProperties.put("eclipselink.cache.shared.default", "false");
        jpaProperties.put("eclipselink.query-results-cache", "false");
        jpaProperties.put("eclipselink.target-database", "MySQL");

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(applicationDataSource());
        entityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
        entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
        entityManagerFactoryBean.setLoadTimeWeaver(loadTimeWeaver);
        entityManagerFactoryBean.setPersistenceUnitName("applicationPersistenceUnit");
        return entityManagerFactoryBean;
    }

    /**
     * A support object that wraps all available repositories and has some
     * convenience functions.
     */
    @Bean
    public Repositories repositories() {
        return new Repositories(applicationContext);
    }
}


@Configuration
@EntityScan(basePackages = { "something.manager.model" })
@EnableJpaAuditing
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = { "something.manager.model" }, repositoryFactoryBeanClass = RepositoryFactoryBean.class, entityManagerFactoryRef = "managerEntityManagerFactory", transactionManagerRef = "managerTransactionManager")
public static class ManagerJpaConfiguration {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * The datasource used by the system. Configuration see
     * /src/main/resources/application.properties.
     */
    @Bean(name="managerDataSource")
    @ConfigurationProperties(prefix = "spring.manager.datasource")
    public DataSource managerDataSource() {
        return new BasicDataSource();
    }

    /**
     * The transaction manager used by the manager system. 
     * 
     * @param javax.sql.DataSource managerDataSource
     * @param javax.sql.DataSource loadTimeWeaver
     * @return org.springframework.orm.jpa.JpaTransactionManager
     */
    @Bean(name="managerTransactionManager")
    public JpaTransactionManager managerTransactionManager(DataSource managerDataSource, LoadTimeWeaver loadTimeWeaver) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(managerEntityManagerFactory(managerDataSource, loadTimeWeaver).getObject());
        return transactionManager;
    }

    /**
     * The entity manager factory used by manager system. Specific properties of the
     * persistence provider eclipselink are configured here.
     */
    @Bean(name="managerEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean managerEntityManagerFactory(DataSource managerDataSource, LoadTimeWeaver loadTimeWeaver) {

        Map<String, String> jpaProperties = new HashMap<String, String>();
        jpaProperties.put("eclipselink.ddl-generation", "create-or-extend-tables");
        jpaProperties.put("eclipselink.session.customizer", HistoryBuildingSessionCustomizer.class.getName());
        jpaProperties.put("eclipselink.logging.level.sql", "fine");
        jpaProperties.put("eclipselink.logging.parameters", "true");
        jpaProperties.put("eclipselink.temporal.mutable", "true");
        jpaProperties.put("eclipselink.persistence-context.flush-mode", "commit");
        jpaProperties.put("eclipselink.cache.shared.default", "false");
        jpaProperties.put("eclipselink.query-results-cache", "false");
        jpaProperties.put("eclipselink.target-database", "MySQL");

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(managerDataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new EclipseLinkJpaVendorAdapter());
        entityManagerFactoryBean.setJpaPropertyMap(jpaProperties);
        entityManagerFactoryBean.setLoadTimeWeaver(loadTimeWeaver);
        return entityManagerFactoryBean;
    }

    /**
     * A support object that wraps all available repositories and has some
     * convenience functions.
     */
    @Bean
    public Repositories repositories() {
        return new Repositories(applicationContext);
    }
}

}

0 个答案:

没有答案