0

我正在尝试直接从 Spring 引导应用程序中超出实体管理器。但是我无法创建包含我需要的方法的类的 bean。我指的是下面的文章。

https://dzone.com/articles/accessing-the-entitymanager-from-spring-data-jpa

ProductCategoryRepositoryCustom 接口:-

public interface ProductCategoryRepositoryCustom {

   public void merge(ProductCategory productcategory);
   public ProductCategory find(int id);
   public void persist(ProductCategory productcategory);

}

ProductCategoryRepositoryImpl实现ProductCategoryRepositoryCustom :-

@Component
public class ProductCategoryRepositoryImpl implements ProductCategoryRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public void merge(ProductCategory productcategory) {
        entityManager.merge(productcategory);
    }

    @Override
    public ProductCategory find(int id) {
        ProductCategory productCategory=entityManager.find(ProductCategory.class,id);
        return productCategory;
    }

    @Override
    public void persist(ProductCategory productcategory) {
          entityManager.persist(productcategory);
    }
}

配置类:-

@Configuration
public class ProductConfig {
    
    @Bean
    public ProductCategoryRepositoryImpl productCategoryRepositoryImpl(){
        return new ProductCategoryRepositoryImpl();
    } 

}

毕竟,当我尝试使用 @Autowire 注释自动连接它时,我无法访问我已经实现的方法。

4

1 回答 1

2

您不需要配置类。由于@Component注释,您的 Repository 已经是一个 bean。

于 2020-09-28T21:06:51.310 回答