Spring Data无法延迟初始化角色的集合,无法初始化代理 – 没有会话。

huangapple go评论97阅读模式
英文:

Spring Data failed to lazily initialize a collection of role, could not initialize proxy - no Session

问题

这是您提供的代码部分的中文翻译:

  1. 我正在使用Spring Boot制作API但似乎无法初始化懒加载集合唯一对我有效的解决方案是将其更改为急加载但这不是一个解决方法
  2. @Entity
  3. @Getter
  4. @Setter
  5. @NoArgsConstructor
  6. @Table(name = "user")
  7. public class UserEntity {
  8. @Id
  9. @GeneratedValue
  10. private Long id;
  11. @Column(unique = true)
  12. String email;
  13. @ElementCollection(fetch = LAZY)
  14. List<String> roles = new ArrayList<>();
  15. public UserEntity(String email) {
  16. this.email = email;
  17. }
  18. }
  19. @Order(Ordered.LOWEST_PRECEDENCE)
  20. @Component
  21. public class AuthFilter extends OncePerRequestFilter {
  22. Logger LOG = LoggerFactory.getLogger(AuthFilter.class);
  23. @Autowired
  24. UserRepository userRepository;
  25. @Override
  26. @Transactional
  27. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  28. throws ServletException, IOException {
  29. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  30. if(authentication != null && authentication.isAuthenticated()){
  31. String email = authentication.getName();
  32. UserEntity user = userRepository.findByEmail(email).orElse(new UserEntity(email));
  33. user.getRoles().clear();
  34. user.getRoles().addAll(authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()));
  35. userRepository.save(user);
  36. }
  37. filterChain.doFilter(request, response);
  38. }
  39. }

这些是您提供的两个类。一个是用户实体,另一个是从认证对象创建用户的过滤器。当调用user.getRoles().clear();时,您会收到著名的错误消息org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ch.ciip.ressources.api.user.UserEntity.roles, could not initialize proxy - no Session

从我的理解来看,Spring Data会为每个懒加载关系创建代理,并且只有在访问时才会获取数据。但要访问它,它需要一个会话(session),显然,Spring在访问代理时无法创建会话,然而,当我在存储库上调用save时,它却没有问题地执行。

我尝试过以下方法:

  • 使用@Transactional,来自org.springframework.transaction.annotation.Transactionaljavax.transaction.Transactional。但这完全没有改变任何事情。
  • 将获取类型更改为EAGER。这个方法有效,但不是我想要的方法。
  • application.properties文件中将enable_lazy_load_no_trans=true更改为true。不推荐此方法,因此我不会尝试它。
  • 在尝试修改之前在懒加载集合上调用一个方法。例如,调用user.getRoles().size();以使其初始化。但这不起作用。
  • 调用Hibernate.initialize(user.getRoles());来初始化集合。但它也不起作用。

我想知道为什么Spring会说No Session,因为它确实为userRepository.findByEmail(email)userRepository.save(user)创建了会话。

我可以手动创建一个EntityManager并使用它来获取/持久化我的实体,但这不是目标,因为我正在使用Spring Data JPA。

英文:

I am making an API with Spring Boot and I never seem to manage to initialize lazy collections. The only solution that has ever worked for me is changing it to eager, but that is no solution.

  1. @Entity
  2. @Getter
  3. @Setter
  4. @NoArgsConstructor
  5. @Table(name = &quot;user&quot;)
  6. public class UserEntity {
  7. @Id
  8. @GeneratedValue
  9. private Long id;
  10. @Column(unique = true)
  11. String email;
  12. @ElementCollection(fetch = LAZY)
  13. List&lt;String&gt; roles = new ArrayList&lt;&gt;();
  14. public UserEntity(String email) {
  15. this.email = email;
  16. }
  17. }
  1. @Order(Ordered.LOWEST_PRECEDENCE)
  2. @Component
  3. public class AuthFilter extends OncePerRequestFilter {
  4. Logger LOG = LoggerFactory.getLogger(AuthFilter.class);
  5. @Autowired
  6. UserRepository userRepository;
  7. @Override
  8. @Transactional
  9. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  10. throws ServletException, IOException {
  11. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  12. if(authentication != null &amp;&amp; authentication.isAuthenticated()){
  13. String email = authentication.getName();
  14. UserEntity user = userRepository.findByEmail(email).orElse(new UserEntity(email));
  15. user.getRoles().clear();
  16. user.getRoles().addAll(authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()));
  17. userRepository.save(user);
  18. }
  19. filterChain.doFilter(request, response);
  20. }
  21. }

These are my two classes. A user entity and a filter that will create user from an authentication object. When the line user.getRoles().clear(); is called, I get the famous error org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ch.ciip.ressources.api.user.UserEntity.roles, could not initialize proxy - no Session.

From my understanding, Spring Data will create a proxy for each lazy relation, and will only fetch the data once it is accessed. But to access it it needs a session and apparently, Spring is not able to create a session when accessing a proxy, however, it has no problem doing it when I call save on a repository.

What I've tried is :

  • @Transactional from both org.springframework.transaction.annotation.Transactional and javax.transaction.Transactional. But it changes absolutely nothing.
  • Change fetch type to EAGER. This method works, but it is not what I want to do.
  • Change enable_lazy_load_no_trans=trueto true in application.properties file. This method is not recommended, so I will not try it.
  • Calling a method on the lazy collection before trying to modify it. For example calling user.getRoles().size(); so it gets initialized. But it does not work.
  • Calling Hibernate.initialize(user.getRoles()); to initialize the collection. But it does not work.

I wonder why Spring says No Session because it does create a session for the userRepository.findByEmail(email) as well as for userRepository.save(user).

I could create an entitymanager manually and use it to fetch/persist my entities. But that is not the goal as I'm using Spring Data JPA.

答案1

得分: 5

将方法设为public,即:

  1. @Override
  2. @Transactional
  3. public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  4. throws ServletException, IOException {
  5. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  6. if (authentication != null && authentication.isAuthenticated()) {
  7. String email = authentication.getName();
  8. UserEntity user = userRepository.findByEmail(email).orElse(new UserEntity(email));
  9. user.getRoles().clear();
  10. user.getRoles().addAll(authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()));
  11. userRepository.save(user);
  12. }
  13. filterChain.doFilter(request, response);
  14. }
英文:

Make the method public, i.e.:

  1. @Override
  2. @Transactional
  3. public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  4. throws ServletException, IOException {
  5. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  6. if(authentication != null &amp;&amp; authentication.isAuthenticated()){
  7. String email = authentication.getName();
  8. UserEntity user = userRepository.findByEmail(email).orElse(new UserEntity(email));
  9. user.getRoles().clear();
  10. user.getRoles().addAll(authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet()));
  11. userRepository.save(user);
  12. }
  13. filterChain.doFilter(request, response);
  14. }

答案2

得分: 2

Creating a separate service class and injecting it into this filter is a workaround I can propose.

  1. @Service
  2. class UserService {
  3. @Autowired
  4. private UserRepository userRepository;
  5. @Transactional
  6. public void findUserByEmailAndAddRoles(String email, Set<String> authorities) {
  7. UserEntity user = userRepository.findByEmail(email)
  8. .orElse(new UserEntity(email));
  9. user.getRoles().clear();
  10. user.getRoles().addAll(authorities);
  11. userRepository.save(user);
  12. }
  13. }

Then use this in your filter

  1. @Override
  2. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  3. throws ServletException, IOException {
  4. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  5. if(authentication != null && authentication.isAuthenticated()){
  6. String email = authentication.getName();
  7. Set<String> authorities = authentication.getAuthorities().stream()
  8. .map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
  9. userService.findUserByEmailAndAddRoles(email, authorities);
  10. }
  11. filterChain.doFilter(request, response);
  12. }
英文:

Creating a separate service class and injecting it into this filter is a workaround i can propose.

  1. @Service
  2. class UserService {
  3. @Autowired
  4. private UserRepository userRepository;
  5. @Transactional
  6. public void findUserByEmailAndAddRoles(String email, Set&lt;String&gt; authorities) {
  7. UserEntity user = userRepository.findByEmail(email)
  8. .orElse(new UserEntity(email));
  9. user.getRoles().clear();
  10. user.getRoles().addAll(authorities);
  11. userRepository.save(user);
  12. }
  13. }

Then use this in your filter

  1. @Override
  2. protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
  3. throws ServletException, IOException {
  4. Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
  5. if(authentication != null &amp;&amp; authentication.isAuthenticated()){
  6. String email = authentication.getName();
  7. Set&lt;String&gt; authorities = authentication.getAuthorities().stream()
  8. .map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
  9. userService.findUserByEmailAndAddRoles(email, authorities);
  10. }
  11. filterChain.doFilter(request, response);
  12. }

huangapple
  • 本文由 发表于 2020年8月5日 17:04:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63261801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定