创建名称为'companyRepositoryImpl'的bean时出错:持久性依赖项注入失败;嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有类型为'javax.persistence.EntityManagerFactory'的合格Bean
我正在通过Spring Security和OAuth2学习Spring REST, 以下是链接: https://dzone.com/articles/secure-spring-rest-with-spring-security-and-oauth2
使用DB作为Postgres。
companyRepositoryImpl.class
@Repository
public class CompanyRepositoryImpl implements CompanyRepository {
//@Autowired
//@Qualifier(value = "transactionManager")
@PersistenceContext
private EntityManager entityManager;
@Override
public Company find(Long id) {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Company> query = builder.createQuery(Company.class);
Root<Company> root = query.from(Company.class);
root.fetch(Company_.cars, JoinType.LEFT);
Fetch<Company, Department> departmentFetch = root.fetch(Company_.departments, JoinType.LEFT);
Fetch<Department, Employee> employeeFetch = departmentFetch.fetch(Department_.employees, JoinType.LEFT);
employeeFetch.fetch(Employee_.address, JoinType.LEFT);
departmentFetch.fetch(Department_.offices, JoinType.LEFT);
query.select(root).distinct(true);
Predicate idPredicate = builder.equal(root.get(Company_.id), id);
query.where(builder.and(idPredicate));
return DataAccessUtils.singleResult(entityManager.createQuery(query).getResultList());
}
HibernateConfiguration.class
package com.adamzareba.spring.security.oauth2.config;
import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.persistence.EntityManagerFactory;
@Configuration
@EnableTransactionManagement
public class HibernateConfiguration {
@Bean
public Module hibernate5Module() {
return new Hibernate5Module();
}
/**
* Creates the transaction manager bean that integrates the used JPA provider with the
* Spring transaction mechanism.
* @param entityManagerFactory The used JPA entity manager factory.
* @return
*/
@Bean
//@Qualifier(value = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
}
我也尝试了以下更改,但是没有运气, 而不是@PersistenceContext
@Autowired
@Qualifier(value = "transactionManager")
private EntityManager entityManager;
和
@Bean
@Qualifier(value = "transactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
}