gpt4 book ai didi

java - 面临无法延迟初始化集合并且无法初始化代理 - 无 session

转载 作者:行者123 更新时间:2023-12-02 09:32:40 24 4
gpt4 key购买 nike

当我从 Angular 发送 post 请求时,然后在 Spring Boot requestfilter 程序中验证 jwt token 时显示错误。

面临的问题org.hibernate.LazyInitializationException:

failed to lazily initialize a collection of role: in.yis.mains.model.OpsUsersLogin.roles, could not initialize proxy - no Session

import java.util.Date;
import java.util.LinkedHashSet;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import javax.persistence.UniqueConstraint;

import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;

@Data
@Entity
@Table(
name="ops_users_login",
uniqueConstraints={@UniqueConstraint(columnNames={"username"})}
)
public class OpsUsersLogin {

@Id
@GeneratedValue( strategy = GenerationType.AUTO )
private long id;

private String name;

@Column(name="username")
private String username;

private String password;

private String oldPassword;

private String status_flag;

private int wrong_attampts;

@JsonFormat(pattern="yyyy-MM-dd")
private Date created_date;

@JsonFormat(pattern="yyyy-MM-dd")
private Date expiry_date;


private long rights;

private String address;

private String designation;

private long empId;

@JsonFormat(pattern="yyyy-MM-dd")
private Date created_by;

@JsonFormat(pattern="yyyy-MM-dd")
private Date updated_by;

@ManyToMany
@JoinTable(
name = "opsUsersLogin_roles",
joinColumns=@JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns=@JoinColumn(name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles;

}


最佳答案

This describes what you need basically

org.hibernate.LazyInitializationException意味着,在您的情况下,相关实体或实体集合 roles ,尚未初始化,因为相关实体的默认获取策略为 LAZY .

要解决此问题,您必须初始化 roles获取 ops_user_login 后的集合实体,或者如果您已经使用一个查询来获取实体,则可以通过查询来获取它们。初始化roles可以用Hibernate.initialize(entity.getRoles())来完成,如果这不起作用您也可以调用 entity.getRoles().size()作为解决方法。

如果您使用查询来选择您的ops_user_login你可以做类似的事情:

Query query = session.createQuery(
"from ops_user_login o " +
"join fetch o.roles"
);

TypedQuery<OpsUsersLogin> query = entityManager.createQuery("Select o from ops_user_login o join fetch o.roles", OpsUsersLogin.class);

关于java - 面临无法延迟初始化集合并且无法初始化代理 - 无 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57821829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com