0

我正在尝试将我的项目与 db 连接起来。我有一个项目已经连接并在这个数据库上运行。我需要第二个和他们之间使用 jms 进行通信。我将 secon 配置为连接到同一个数据库,但它不工作。连接正常。当我创建实体类然后在数据库中我看到新表。很好。但是我编写了应该注入实体管理器的控制器,它始终为空。我不知道为什么。这是我的配置。

@Configuration
@ComponentScan(basePackages = { "**.*****.**********" })
public class CalculationWorkerRootConfig {

    @Bean
    public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setLocation(new ClassPathResource("/persistence.properties"));
        return ppc;
    }

} 

这是管理器配置器:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories
public class CalculationWorkerPersistenceConfig implements TransactionManagementConfigurer {

    @Value("${dataSource.driverClassName}")
    private String driver;
    @Value("${dataSource.url}")
    private String url;
    @Value("${dataSource.username}")
    private String username;
    @Value("${dataSource.password}")
    private String password;
    @Value("${hibernate.dialect}")
    private String dialect;
    @Value("${hibernate.hbm2ddl.auto}")
    private String hbm2ddlAuto;

@Bean
public DataSource configureDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(driver);
    dataSource.setUrl(url);
    dataSource.setUsername(username);
    dataSource.setPassword(password);
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean configureEntityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(configureDataSource());
    entityManagerFactoryBean.setPackagesToScan("**.*********.************");
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    Properties jpaProperties = new Properties();
    jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect);
    jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto);
    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    return entityManagerFactoryBean;
}

@Bean   
public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new JpaTransactionManager();
}

}

这是存储库:

@Repository
@Transactional(readOnly = true)
public class CalculationWorkerRepository {

@PersistenceContext
EntityManager entityManager; 


public Campaign findByID(Long id) {
    return entityManager.find(Campaign.class, id);
}

@Transactional
public void setCalculating(Boolean calculating, Long campaignID) {
    Campaign campaign = findByID(campaignID);
    campaign.setCalculating(calculating);
}

}
4

3 回答 3

8

班级LocalContainerEntityManagerFactoryBean是工厂。您会从javadoc中注意到它扩展AbstractEntityManagerFactoryBean了提供方法afterPropertiesSet()getObject(). 你需要调用这些来实例化一个EntityManagerFactory.

@Bean
public EntityManagerFactory configureEntityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(configureDataSource());
    entityManagerFactoryBean.setPackagesToScan("**.*********.************");
    entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

    Properties jpaProperties = new Properties();
    jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect);
    jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto);
    entityManagerFactoryBean.setJpaProperties(jpaProperties);

    entityManagerFactoryBean.afterPropertiesSet();
    return (EntityManagerFactory) entityManagerFactoryBean.getObject();
}

以上将创建一个EntityManagerFactory. 然后你需要一个EntityManager豆子。

@Bean
public EntityManager entityManager() {
    return configureEntityManagerFactory.createEntityManager();
}
于 2013-06-12T15:43:40.310 回答
1

从我看到的情况来看,您只是在实例化 anEntityManagerFactory而不是 an EntityManager,因此您必须在启动时注入EntityManagerFactory或 instatntiate anEntityManager才能注入。

还要注意这篇文章中描述的与事务和不同环境相关的 EM 注入问题。

于 2013-06-12T14:43:27.187 回答
0

问题是JpaTransactionManager缺少对EntityManagerFactory. 使用构造函数注入或设置器注入来解决它:

@Bean   
public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new JpaTransactionManager(configureEntityManagerFactory());
}

或者

@Bean   
public PlatformTransactionManager annotationDrivenTransactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(configureEntityManagerFactory());
    return transactionManager;
}

参考,在 Spring 参考文档中JPA Transaction 段落JpaTransactionManager中的 XML 应用上下文中有相应的配置。

于 2013-06-12T18:04:59.080 回答