How to access Spring Data configured entity manager (factory) in custom implementation

后端 未结 3 963
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 22:57

I\'m writing a custom implementation for a Spring Data JPA repository. So I have:

  • MyEntityRepositoryCustom => interface with the custom methods
3条回答
  •  悲&欢浪女
    2020-12-23 23:48

    The best I could find is to set up a "convention": my repositories declare that they expect a persistence unit named myConventionalPU to be made available. The application layer then assigns that alias to the entity manager factory that it sets up and injects into Spring Data, so my custom implementations can receive the correct EMF with autowiring by using that alias. Here's an excerpt of my application context:

    
      [...]
    
    
    
    

    And within my custom implementation:

    @PersistenceContext(unitName = "myConventionalPU")
    private EntityManager em;
    

    I opened DATAJPA-669 with this requirement.

提交回复
热议问题