我正在尝试将我的项目与 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);
}
}