拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 如何修复无法延迟初始化集合

如何修复无法延迟初始化集合

白鹭 - 2022-03-02 2091 0 0

当我尝试从数据库中获取有关用户的资料时,出现以下错误:

Request processing failed; nested exception is org.hibernate.LazyInitializationException: failed
    to lazily initialize a collection of role: com.hramyko.finalapp.entity.CommonUser.comments, could not initialize
    proxy - no Session

我知道这发生在我的会话关闭之前,但我不想使用 Eager fetch。有什么方法可以解决我的问题?我在stackoverflow上阅读了我的问题的解决方案,但它对我没有帮助。

我的 CommonUser 类:

package com.hramyko.finalapp.entity;

import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Entity
@Table(name = "common_users")
public class CommonUser extends User {

@OneToMany(mappedBy = "author")
private List<Comment> comments;

public CommonUser() {
}

public CommonUser(User user) {
    super(user.getEmail(), user.getPassword(), user.getFirstName(), user.getLastName(),
            user.getCreatedAt(), user.getRole(), user.getStatus());
    this.id = user.getId();
}

public CommonUser(String email, String password, String firstName,
              String lastName, Date createdAt, Role role, Status status, List<Comment> comments) {
    super(email, password, firstName, lastName, createdAt, role, status);
    this.comments = comments;
}

public List<Comment> getComments() {
    return comments;
}

public void setComments(List<Comment> comments) {
    this.comments = comments;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    if (!super.equals(o)) return false;
    CommonUser that = (CommonUser) o;
    return Objects.equals(comments, that.comments);
}

@Override
public int hashCode() {
    return Objects.hash(super.hashCode(), comments);
}

@Override
public String toString() {
    return "CommonUser{"  
            "id="   id  
            ", email='"   email   '\''  
            ", firstName='"   firstName   '\''  
            ", lastName='"   lastName   '\''  
            ", comments="   comments  
            ", createdAt="   createdAt  
            ", role="   role  
            '}';
}

}

服务:

@Transactional
@Override
public User currentUser() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    String username;
    if (principal instanceof UserDetails) {
        username = ((UserDetails) principal).getUsername();
    } else {
        username = principal.toString();
    }
    return userRepository.findUserByEmail(username);
}

@Transactional
@Override
public User findUserByEmail(String email) {
    userValidator.validateEmail(email);
    return userRepository.findUserByEmail(email);
}

控制器:

@PreAuthorize("hasAnyAuthority('user.read', 'user.write', 'user.delete')")
@GetMapping("my_account")
public String show() {
    return userService.currentUser().toString();
}

uj5u.com热心网友回复:

这是因为您正在访问用户的评论,toString()但评论尚未初始化,加载用户的事务已经关闭。

通常有两种解决方法:

  1. 在加载此用户时使用fetch join来获取评论

  2. 增加事务边界以覆写控制器的 show() 方法,这样当您user.toString()在其中访问时,加载用户的事务仍然处于活动状态且尚未关闭:

@Transactional
public String show() {
    return userService.currentUser().toString();
}
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *