I'm trying to connect my project with db. I have one project that is already connected and operates on this db. And i need second one and communication between them using jms. I configure secon to connect to the same db and it isn`t working. Well connection is ok. When i create Entity class then in db i see new table. So great. But i wrote controller that should inject entity manager and it's always null. i don't have idea why. Here is my config.
@Configuration
@ComponentScan(basePackages = { "**.*****.**********" })
public class CalculationWorkerRootConfig {
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource("/persistence.properties"));
return ppc;
}
}
Here is 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();
}
}
And here is repository:
@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);
}
}