spring-projects / spring-projects/spring-data-rest

Custom OpenEntityManagerInViewInterceptor [DATAREST-719]

Open
#1,088 0 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Dec 31, 2020.

type: enhancement
Dominant language
Java
Stars
958
Forks
568
PR merge metrics
No merged PRs in 30d

Description

Mathias D opened DATAREST-719 and commented

We are using a custom OpenEntityManagerInViewInterceptor to bring in tenant related properties that we are using for Tenancy on JPA level. This was pretty easy to register in a spring-boot/spring-web-mvc based application.

This was our approach with spring-web-mvc:

@Configuration
    @ConditionalOnWebApplication
    static class TenantAwareOpenEntityManagerInViewConfiguration extends WebMvcConfigurerAdapter {

        @Autowired
        private TenantJpaPropertyMapBuilder jpaPropertyMapBuilder;

        @Bean
        public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
            return new TenantAwareOpenEntityManagerInViewInterceptor(jpaPropertyMapBuilder);
        }

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
        }
    }

With spring-data-rest this does not work anymore:

@Configuration
    @ConditionalOnWebApplication
    static class TenantAwareOpenEntityManagerInViewConfiguration extends RepositoryRestMvcConfiguration {

        @Autowired
        private TenantJpaPropertyMapBuilder jpaPropertyMapBuilder;

        @Bean
        public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
            return new TenantAwareOpenEntityManagerInViewInterceptor(jpaPropertyMapBuilder);
        }

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
        }
    }

The addInterceptors method is called but our TenantAwareOpenEntityManagerInViewInterceptor is not used.

I think the reason is that spring-data-rest always creates and registers the standard org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor in their org.springframework.data.rest.webmvc.support.JpaHelper. We think it should not create the interceptor but get the bean from the application context.

Our workaround is to extend the JpaHelper and override the jpaHelper() method in RepositoryRestMvcConfiguration.

This looks like this:

@Configuration
    @ConditionalOnWebApplication
    static class TenantAwareOpenEntityManagerInViewConfiguration extends RepositoryRestMvcConfiguration {

        @Autowired
        private TenantJpaPropertyMapBuilder jpaPropertyMapBuilder;

        @Override
        @Bean
        public TenantJpaHelper jpaHelper() {
            return new TenantJpaHelper(openEntityManagerInViewInterceptor());
        }

        @Bean
        public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
            return new TenantAwareOpenEntityManagerInViewInterceptor(jpaPropertyMapBuilder);
        }

        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            registry.addWebRequestInterceptor(openEntityManagerInViewInterceptor());
        }
    }

We think that this is not very robust.

It would be convenient to use the same mechanism to customize the OpenEntityManagerInViewInterceptor in spring-data-rest as in spring-web-mvc


Affects: 2.4.1 (Gosling SR1)

1 votes, 1 watchers

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.