布线问题。 org.hibernate.LazyInitializationException:无法懒惰地初始化集合

时间:2015-01-12 15:18:03

标签: java spring hibernate spring-mvc spring-security

尝试使用getUserRole()

时会遇到一些异常
org.apache.jasper.JasperException: An exception occurred processing JSP page /WEB-INF/jsp/index.jsp at line 19

16: }
17: */
18: User user = (User)request.getAttribute("list");
19: out.print(user.getLogin() + " : " + user.getPassword() + " : " + user.getUserRole());
20: %>

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.myproject.User.userRole, could not initialize proxy - no Session
    org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:575)
    org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:214)
    org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:554)
    org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:142)
    org.hibernate.collection.internal.PersistentSet.toString(PersistentSet.java:316)
    java.lang.String.valueOf(String.java:2847)
    java.lang.StringBuilder.append(StringBuilder.java:128)
    org.apache.jsp.WEB_002dINF.jsp.index_jsp._jspService(index_jsp.java:90)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

这里我的注释类:

@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;
        }

    }

并结束我的DAO:

@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;
    }

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

1 个答案:

答案 0 :(得分:0)

您可以从LAZY to EAGER

进行更改
   fetch = FetchType.EAGER

或使用Hibernate.intialize()初始化userRole

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