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

后端 未结 3 865
伪装坚强ぢ
伪装坚强ぢ 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:40

    Since version Spring Data JPA 1.9.2 you have access to EntityManager through JpaContext, see: http://docs.spring.io/spring-data/jpa/docs/1.9.2.RELEASE/reference/html/#jpa.misc.jpa-context. Example:

    @Component
    public class RepositoryUtil
    {
        @Autowired
        private JpaContext jpaContext;
    
        public void deatach(T entity)
        {
            jpaContext.getEntityManagerByManagedType(entity.getClass()).detach(entity);
        }
    }
    

    P.S. This approach will not work if you have more than one EntityManager candidate for some Class, see implementation of JpaContext#getEntityManagerByManagedType -> DefaultJpaContext#getEntityManagerByManagedType.

    0 讨论(0)
  • 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:

    <bean id="myEntityManagerFactory" name="myConventionalPU" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      [...]
    </bean>
    
    <jpa:repositories base-package="com.example"
      entity-manager-factory-ref="myEntityManagerFactory"
      transaction-manager-ref="transactionManager" />
    

    And within my custom implementation:

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

    I opened DATAJPA-669 with this requirement.

    0 讨论(0)
  • 2020-12-23 23:49

    Spring Data JPA uses Auto configuration classes to auto generate entityManagerFactory, dataSource and transactionManager.

    If you want get access to entityManager and control the instantiation and settings, you need to define your own PersistenceConfiguration. Below is the sample code using Java Config

        @Configuration
        @EnableTransactionManagement
        @EnableJpaRepositories(basePackages = { "com.test.repositories.*" })
        public class PersistenceJpaConfig {
    
            @Autowired
            JpaVendorAdapter jpaVendorAdapter;
    
            @Bean
            public DataSource dataSource() {
                return new EmbeddedDatabaseBuilder()
                        .setName("testdb")
                        .setType(EmbeddedDatabaseType.HSQL)
                        .build();
            }
    
            @Bean
            public EntityManager entityManager() {
                return entityManagerFactory().createEntityManager();
            }
    
            @Bean
            public EntityManagerFactory entityManagerFactory() {
                LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
                lef.setDataSource(dataSource());
                lef.setJpaVendorAdapter(jpaVendorAdapter);
                lef.setPackagesToScan("com.test.domain.*");
                lef.afterPropertiesSet();
                return lef.getObject();
            }
    
            @Bean
            public PlatformTransactionManager transactionManager() {
                return new JpaTransactionManager(entityManagerFactory());
            }
        }
    

    If you have multiple data sources, follow this article.

    0 讨论(0)
提交回复
热议问题