Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 布线问题。org.hibernate.LazyInitializationException:延迟初始化集合失败_Java_Spring_Hibernate_Spring Mvc_Spring Security - Fatal编程技术网

Java 布线问题。org.hibernate.LazyInitializationException:延迟初始化集合失败

Java 布线问题。org.hibernate.LazyInitializationException:延迟初始化集合失败,java,spring,hibernate,spring-mvc,spring-security,Java,Spring,Hibernate,Spring Mvc,Spring Security,我在尝试getUserRole()时发现了一些异常 以下是我的课堂注释: @Entity @Table(name="users") public class User implements Serializable{ private static final long serialVersionUID = 1L; private String login; private String password; private String email; p

我在尝试getUserRole()时发现了一些异常

以下是我的课堂注释:

@Entity
@Table(name="users")
public class User implements Serializable{

    private static final long serialVersionUID = 1L;

    private String login;
    private String password;
    private String email;
    private boolean enabled;
    private Set<UserRole> userRole = new HashSet<UserRole>(0);

    public User(){}
    public User(String login, String password, String email, boolean enabled){
        this.login = login;
        this.password = password;
        this.email = email;
        this.enabled = enabled;
    }
    public User(String login, String password, String email, boolean enabled, Set<UserRole> userRole){
        this.login = login;
        this.password = password;
        this.email = email;
        this.enabled = enabled;
        this.userRole = userRole;
    }

    @Id
    @Column(name="login", unique=true, nullable=false, length = 50)
    public String getLogin(){
        return this.login;
    }

    public void setLogin(String login){
        this.login = login;
    }

    @Column(name="password", nullable = false, length = 50)
    public String getPassword(){
        return this.password;
    }

    public void setPassword(String password){
        this.password = password;
    }

    @Column(name = "email", nullable = false, length = 100)
    public String getEmail(){
        return this.email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    @Column(name = "enabled", nullable = false)
    public boolean isEnabled(){
        return this.enabled;
    }

    public void setEnabled(boolean enabled){
        this.enabled = enabled;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
    public Set<UserRole> getUserRole(){
        return this.userRole;
    }

    public void setUserRole(Set<UserRole> userRole){
        this.userRole = userRole;
    }

}

    @Entity
    @Table(name="user_roles", uniqueConstraints = @UniqueConstraint(columnNames = {"role","login"}))
    public class UserRole implements Serializable{

        private static final long serialVersionUID = 1L;

        private int roleId;
        private User user;
        private String role;

        public UserRole(){}
        public UserRole(User user, String role){
            this.user = user;
            this.role = role;
        }

        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "role_id", unique = true, nullable = false)
        public int getRoleId(){
            return this.roleId;
        }

        public void setRoleId(int roleId){
            this.roleId = roleId;
        }

        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name = "login", nullable = false)
        public User getUser(){
            return this.user;
        }

        public void setUser(User user){
            this.user = user;
        }

        @Column(name = "role", nullable = false)
        public String getRole(){
            return this.role;
        }

        public void setRole(String role){
            this.role = role;
        }

    }
@实体
@表(name=“users”)
公共类用户实现可序列化{
私有静态最终长serialVersionUID=1L;
私有字符串登录;
私有字符串密码;
私人字符串电子邮件;
启用私有布尔值;
私有集userRole=newhashset(0);
公共用户(){}
公共用户(字符串登录、字符串密码、字符串电子邮件、布尔启用){
this.login=登录;
this.password=密码;
this.email=电子邮件;
this.enabled=已启用;
}
公共用户(字符串登录、字符串密码、字符串电子邮件、启用布尔值、设置用户角色){
this.login=登录;
this.password=密码;
this.email=电子邮件;
this.enabled=已启用;
this.userRole=userRole;
}
@身份证
@列(name=“login”,unique=true,nullable=false,length=50)
公共字符串getLogin(){
返回此.login;
}
公共void setLogin(字符串登录){
this.login=登录;
}
@列(name=“password”,null=false,长度=50)
公共字符串getPassword(){
返回此密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
@列(name=“email”,null=false,长度=100)
公共字符串getEmail(){
返回此电子邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
@列(name=“enabled”,nullable=false)
公共布尔值isEnabled(){
返回此参数。已启用;
}
已启用公共void集(已启用布尔值){
this.enabled=已启用;
}
@OneToMany(fetch=FetchType.LAZY,mappedBy=“user”)
公共集getUserRole(){
返回this.userRole;
}
public void setUserRole(Set userRole){
this.userRole=userRole;
}
}
@实体
@表(name=“user_roles”,uniqueConstraints=@UniqueConstraint(columnNames={“role”,“login”}))
公共类UserRole实现可序列化{
私有静态最终长serialVersionUID=1L;
私人内部控制;
私人用户;
私有字符串角色;
公共用户角色(){}
公共用户角色(用户用户、字符串角色){
this.user=用户;
this.role=角色;
}
@身份证
@生成值(策略=标识)
@列(name=“role\u id”,unique=true,nullable=false)
public int getRoleId(){
返回此.roleId;
}
公共void setRoleId(int-roleId){
this.roleId=roleId;
}
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“login”,nullable=false)
公共用户getUser(){
返回此.user;
}
公共void setUser(用户){
this.user=用户;
}
@列(name=“role”,nullable=false)
公共字符串getRole(){
返回这个角色;
}
公共void setRole(字符串角色){
this.role=角色;
}
}
结束我的道:

@Override
    @Transactional
    @SuppressWarnings("unchecked")
    public User findByLogin(String login){
        String query = "from User where login = ?";
        List<User> listUser = sessionFactory.getCurrentSession().createQuery(query).setParameter(0, login).list();

        if(listUser.size() > 0)
            return listUser.get(0);
        else
            return null;
    }
@覆盖
@交易的
@抑制警告(“未选中”)
公共用户findByLogin(字符串登录){
String query=“来自登录名=?”的用户;
List listUser=sessionFactory.getCurrentSession().createQuery(查询).setParameter(0,登录).List();
if(listUser.size()>0)
返回listUser.get(0);
其他的
返回null;
}

必须在没有user.getUserRole()的情况下标记jsp才能正常工作,但当我新插入此方法片段时,我再次看到错误。在这个项目中也实现了SpringSecurity,但是“登录”不起作用。我想问题是一样的

你可以从
懒惰变为渴望

   fetch = FetchType.EAGER
或者使用
Hibernate.initialize()
初始化
userRole

 if(listUser.size() > 0)
           User user = listUser.get(0);
Hibernate.initialize(user.getUserRole());
        else
            return null;

你的收藏被偷拍了。在检索到
用户后
您的hibernate会话将消失,因此无法懒洋洋地获取集合。(例外情况也告诉您)。让您的集合立即获取,或者使用
OpenSessionInViewFilter
,或者创建一个特定的HQL,强制检索此屏幕的角色集合。
 if(listUser.size() > 0)
           User user = listUser.get(0);
Hibernate.initialize(user.getUserRole());
        else
            return null;