EntityManager为null。不能注射

时间:2013-06-12 14:05:05

标签: spring hibernate jpa entitymanager

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

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

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

} 

这是manager configurer:

@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);
}

}

3 个答案:

答案 0 :(得分:7)

班级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。

@Bean
public EntityManager entityManager() {
    return configureEntityManagerFactory.createEntityManager();
}

答案 1 :(得分:1)

从我看到你只是实例化一个EntityManagerFactory而不是一个EntityManager,所以要么你必须注入EntityManagerFactory或者在启动时注册EntityManager可以注射。

另请注意this帖子中描述的与交易和不同环境相关的EM注入问题。

答案 2 :(得分:0)

问题是JpaTransactionManager缺少对EntityManagerFactory的引用。使用构造函数注入或setter注入来解决它:

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

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

引用,Spring参考文档中JPA Transaction paragraph的XML应用程序上下文中的JpaTransactionManager具有相应的配置。