在过滤器中访问spring boot数据jpa实体 [英] Accessing spring boot data jpa entity in filter

查看:179
本文介绍了在过滤器中访问spring boot数据jpa实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试将基于servlet的应用程序转换为带控制器的spring启动应用程序。设置过滤器后,我在访问实体函数时遇到无法初始化代理 - 无会话-Exception。 (这里:用户方法isAdmin)。

At the moment I'm trying to convert my servlet-based application to a spring boot application with controllers. After I set up a filter I encountered an "could not initialize proxy - no Session"-Exception, when accessing an entities function. (Here: The users method "isAdmin").

我设置了这样的过滤器:

I set up a filter like this:

public class AdminFilter implements Filter {

  @Autowired
  UserRepository userRepository;

  @Override
  @Transactional
  public void doFilter(ServletRequest aRequest, ServletResponse aResponse, FilterChain aFilterChain)
        throws IOException, ServletException {
            User u = userRepository.getOne(5l).orElse(null);
            System.out.println(u.isAdmin());
    aFilterChain.doFilter(aRequest, aResponse);
  }

  @Override
  public void init(FilterConfig filterConfig) throws ServletException {
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
            filterConfig.getServletContext());
  }

  @Override
  public void destroy() {
  }
}

实现Filter接口并在@Configuration标记类中添加Filter以将过滤器注册到/ admin:

Implemented the Filter interface and addded the Filter in an @Configuration tagged class to register the filter to "/admin":

@Configuration
public class SpringConfiguration {
@Bean
public FilterRegistrationBean adminRegistration() {
    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(new AdminFilter());
    registration.addUrlPatterns("/admin/*");
    return registration;
   }
}

通过以下语句启用自动装配支持:

The Autowiring-Support is enabled by the following statement:

SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
        filterConfig.getServletContext());

我尝试将@Transactional添加到方法中并添加了OpenSessionInViewFilter - 但没有成功。

I tried to add @Transactional to the method and also added the OpenSessionInViewFilter - without success.

在我的Controller-Beans(@Controller)中,我可以访问从自动装配的存储库中检索到的实体的功能而没有任何问题。

In my Controller-Beans (@Controller) I can access the function of the entity retrieved from an autowired repository without any problems.

该函数只返回用户实体的值:

The function just returns a value from the user entity:

public boolean isAdmin() {
    return admin;
}

为什么我收到错误以及如何修复错误?

Any ideas why I'm getting the error and how to fix it?

一个stracktrace cut:

A stracktrace cut:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:148) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:266) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73) ~[hibernate-core-5.0.11.Final.jar:5.0.11.Final]
at de.seeme.web.persistence.User_$$_jvst43d_a.isAdmin(User_$$_jvst43d_a.java) ~[bin/:na]

问候

gemorra

推荐答案

该死的。尝试使用userRepository.find(id),一切正常。

damn. Tried it with userRepository.find(id) and everything works fine.

使用getOne,您只需获得对仅包含id的实体的引用(用于存在检查或类似)。

With getOne you will just get a reference to the entity containing only the id (for existence checks or similiar).

CrudRepository findOne()与JpaRepository getOne()之间的区别

这篇关于在过滤器中访问spring boot数据jpa实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆