0

I am trying to excess Entity Manager directly from Spring boot application. But I am unable to create bean of a class containing methods which I need. I am referring from the following article.

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

ProductCategoryRepositoryCustom Intraface:-

public interface ProductCategoryRepositoryCustom {

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

}

ProductCategoryRepositoryImpl implements 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 Class:-

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

}

After all these thing when I am trying to Auto-wire it with @Autowire annotation I am unable to access the methods which i have implemented.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
deep
  • 143
  • 3
  • 12

1 Answers1

2

You don't need a configuration class. Your Repository is a bean already due to @Component annotation.

Boris
  • 1,054
  • 1
  • 13
  • 23
  • sir what only i am getting is getter and setter for `ProductCategoryRepositoryImpl` in class where i have Autowired it. – deep Sep 28 '20 at 21:32
  • I didn't get your last message. Does it work if you remove ProductConfig? – Boris Sep 28 '20 at 21:40
  • Sorry i was wrong it is working fine according to my expectation after removing ProductConfig.Thanks.Can u please explain what was happening if ProductConfig file was there internally. If you don't have problem By the way thanks – deep Sep 28 '20 at 21:53
  • ProductConfig was creating a fresh new instance of ProductCategoryRepositoryImpl. This instance was not involved into spring context and **@PersistenceContext** annotation was just ignored. – Boris Sep 28 '20 at 21:57