如何修复Lazy初始化异常

时间:2014-08-01 02:50:27

标签: json spring hibernate lazy-initialization

请求处理失败;嵌套异常是org.hibernate.LazyInitializationException:懒得初始化一个角色集合:com.softforge.domain.UserAcc.testAccList,没有会话或会话被关闭

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.softforge.domain;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author sathyagriffin
 */
@XmlRootElement
@Entity
@Table(name = "user_acc")
@NamedQueries({
    @NamedQuery(name = "UserAcc.findAll", query = "SELECT u FROM UserAcc u")})
public class UserAcc implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "user_id")
    private Integer userId;
    @Basic(optional = false)
    @Lob
    @Column(name = "user_name")
    private String userName;
    @Basic(optional = false)
    @Lob
    @Column(name = "Company")
    private String company;
    @Basic(optional = false)
    @Column(name = "user_email")
    private String userEmail;
    @Basic(optional = false)
    @Column(name = "is_account_activated")
    private boolean isAccountActivated;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assigenedToId",fetch=FetchType.LAZY)
    private List<Issues> issuesList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assosiateId",fetch=FetchType.LAZY)
    private List<Projectdetails> projectdetailsList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "moduleassignedtoid",fetch=FetchType.LAZY)
    private List<DevelAcc> develAccList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "assignedUserId",fetch=FetchType.LAZY)
    private List<TestAcc> testAccList;

    public UserAcc() {
    }

    public UserAcc(Integer userId) {
        this.userId = userId;
    }

    public UserAcc(Integer userId, String userName, String company, String userEmail, boolean isAccountActivated) {
        this.userId = userId;
        this.userName = userName;
        this.company = company;
        this.userEmail = userEmail;
        this.isAccountActivated = isAccountActivated;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    public boolean getIsAccountActivated() {
        return isAccountActivated;
    }

    public void setIsAccountActivated(boolean isAccountActivated) {
        this.isAccountActivated = isAccountActivated;
    }

    public List<Issues> getIssuesList() {
        return issuesList;
    }

    public void setIssuesList(List<Issues> issuesList) {
        this.issuesList = issuesList;
    }

    public List<Projectdetails> getProjectdetailsList() {
        return projectdetailsList;
    }

    public void setProjectdetailsList(List<Projectdetails> projectdetailsList) {
        this.projectdetailsList = projectdetailsList;
    }

    public List<DevelAcc> getDevelAccList() {
        return develAccList;
    }

    public void setDevelAccList(List<DevelAcc> develAccList) {
        this.develAccList = develAccList;
    }

    public List<TestAcc> getTestAccList() {
        return testAccList;
    }

    public void setTestAccList(List<TestAcc> testAccList) {
        this.testAccList = testAccList;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (userId != null ? userId.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof UserAcc)) {
            return false;
        }
        UserAcc other = (UserAcc) object;
        if ((this.userId == null && other.userId != null) || (this.userId != null && !this.userId.equals(other.userId))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.softforge.domain.UserAcc[ userId=" + userId + " ]";
    }

}

我如何解决这个问题......懒惰的初始化异常

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/app-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>Softland</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/Softland-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Softland</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet>
    <servlet-name>Rest</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>com.softforge.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Rest</servlet-name>
    <url-pattern>/rest/*</url-pattern>
  </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

答案很简单:从FetchType

更改LAZY
@OneToMany(cascade = CascadeType.ALL, mappedBy = "assignedUserId",fetch=FetchType.LAZY)
private List<TestAcc> testAccList;

EAGER

@OneToMany(cascade = CascadeType.ALL, mappedBy = "assignedUserId",fetch=FetchType.EAGER)
private List<TestAcc> testAccList;

答案很难:性能/易用性方面的最佳方法在很大程度上取决于您的使用案例。为了能够做出明智的决定,我建议你阅读Hibernate lazy / eager加载策略。将FetchTypeLAZY更改为EAGER的性能影响非常大。