当前位置: 首页 > 知识库问答 >
问题:

原因:组织。冬眠LazyInitializationException:未能延迟初始化角色集合:

衡安晏
2023-03-14
   package com.ccdm.vo;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * ViewModel generated by hbm2java
 */
@Entity
@Table(name = "lb", catalog = "ccdmdb")
public class LoadBal implements java.io.Serializable {

    private static final long serialVersionUID = -767868186084580713L;
    private Integer lbID;
    @JsonIgnore
    private AppUser appUser;
    private ViewModel templates;
    private String lbname;
    private String lbdescription;
    private String lbtype;
    private int clientId;



    public LoadBal() {
    }

    public LoadBal(AppUser appUser, String lbname, String lbdescription, String lbtype, ViewModel templates,
            int clientId) {
        this.appUser = appUser;
        this.lbname = lbname;
        this.lbdescription = lbdescription;
        this.lbtype = lbtype;
        this.templates = templates;
        this.clientId = clientId;
        System.out.println(this.lbname);
        System.out.println(this.lbdescription);
        System.out.println(this.lbtype);
        System.out.println(this.templates);
        System.out.println(this.clientId);
    }



    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "LB_ID", unique = true, nullable = false)
    public Integer getlbID() {
        return this.lbID;
    }

    public void setlbID(Integer lbID) {
        this.lbID = lbID;
    }
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID", nullable = false)
    public AppUser getAppUser() {
        return this.appUser;
    }

    public void setAppUser(AppUser appUser) {
        this.appUser = appUser;
    }
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VM_ID", nullable = false)
    public ViewModel getTemplates() {
        System.out.println("here2" + templates);
        return this.templates;
    }

    public void setTemplates(ViewModel VmTemplates) {
        System.out.println("here3" + VmTemplates);
        this.templates= VmTemplates;
    }


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

    public void setlbname(String lbname) {
        this.lbname = lbname;
    }
    @Column(name = "LB_DESCRIPTION", nullable = false, length = 100)
    public String getlbdescription() {
        return this.lbdescription;
    }

    public void setlbdescription(String lbdescription) {
        this.lbdescription = lbdescription;
    }
    @Column(name = "LB_TYPE", nullable = false, length = 100)
    public String getlbtype() {
        return this.lbtype;
    }

    public void setlbtype(String lbtype) {
        this.lbtype = lbtype;
    }
    @Column(name = "CLIENT_ID", nullable = false)
    public int getClientId() {
        return this.clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }



}














package com.ccdm.vo;
// Generated 19 Dec, 2016 5:50:57 PM by Hibernate Tools 5.2.0.CR1

import static javax.persistence.GenerationType.IDENTITY;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

/**
 * ViewModel generated by hbm2java
 */
@Entity
@Table(name = "view_model", catalog = "ccdmdb")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ViewModel implements java.io.Serializable {

    private static final long serialVersionUID = -7778681960884580713L;
    private Integer vmId;
    @JsonIgnore
    private AppUser appUser;
    @JsonIgnore
    private OperatingSystem operatingSystem;
    @JsonIgnore
    private VirtualMachine virtualMachine;
    private String name;
    private int clientId;
    @JsonIgnore
    private Set<SolutionVmMap> solutionVmMaps = new HashSet<SolutionVmMap>(0);
    @JsonIgnore
    private Set<LoadBal> VmTemplates = new HashSet<LoadBal>(0);

    public ViewModel() {
    }

    public ViewModel(AppUser appUser, OperatingSystem operatingSystem, VirtualMachine virtualMachine, String name,
            int clientId) {
        this.appUser = appUser;
        this.operatingSystem = operatingSystem;
        this.virtualMachine = virtualMachine;
        this.name = name;
        this.clientId = clientId;
    }

    public ViewModel(AppUser appUser, OperatingSystem operatingSystem, VirtualMachine virtualMachine, String name,
            int clientId, Set<SolutionVmMap> solutionVmMaps, Set<LoadBal> VmTemplates) {
        this.appUser = appUser;
        this.operatingSystem = operatingSystem;
        this.virtualMachine = virtualMachine;
        this.name = name;
        this.clientId = clientId;
        this.solutionVmMaps = solutionVmMaps;
        this.VmTemplates = VmTemplates;
        System.out.println("vmtem------" + VmTemplates);

    }


    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "VM_ID", unique = true, nullable = false)
    public Integer getVmId() {
        return this.vmId;
    }

    public void setVmId(Integer vmId) {
        this.vmId = vmId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "USER_ID", nullable = false)
    public AppUser getAppUser() {
        return this.appUser;
    }

    public void setAppUser(AppUser appUser) {
        this.appUser = appUser;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "OS_ID", nullable = false)
    public OperatingSystem getOperatingSystem() {
        return this.operatingSystem;
    }

    public void setOperatingSystem(OperatingSystem operatingSystem) {
        System.out.println("os" + operatingSystem);
        this.operatingSystem = operatingSystem;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "VIRTUAL_MACHINE_ID", nullable = false)
    public VirtualMachine getVirtualMachine() {
        return this.virtualMachine;
    }

    public void setVirtualMachine(VirtualMachine virtualMachine) {
        this.virtualMachine = virtualMachine;
    }

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

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "CLIENT_ID", nullable = false)
    public int getClientId() {
        return this.clientId;
    }

    public void setClientId(int clientId) {
        this.clientId = clientId;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "viewModel")
    public Set<SolutionVmMap> getSolutionVmMaps() {
        return this.solutionVmMaps;
    }

    public void setSolutionVmMaps(Set<SolutionVmMap> solutionVmMaps) {
        this.solutionVmMaps = solutionVmMaps;
    }
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "templates")
    public Set<LoadBal> getTemplates() {
        return this.VmTemplates;
    }

    public void setTemplates(Set<LoadBal> VmTemplates) {
        this.VmTemplates = VmTemplates;
    }



}

嗯,我是Spring的新手。当我试图编译这些代码时,我得到了下面提到的错误。所以请帮我解决这个问题。提前感谢!

org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role:      com.ccdm.vo.ViewModel.templates, could not initialize proxy - no Session (through reference chain: com.ccdm.infrastructure.model.CCDMResponse["content"]->java.util.ArrayList[0]->com.ccdm.vo.ViewModel["templates"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.ccdm.vo.ViewModel.templates, could not initialize proxy - no Session (through reference chain: 
        \\\com.ccdm.infrastructure.model.CCDMResponse["content"]->java.util.ArrayList[0]->com.ccdm.vo.ViewModel["templates"])

共有2个答案

葛阳华
2023-03-14

LazyLaunalizationException在您获取@ManyTo很多@OneTo很多对象时发生

Hibernate在(@manytomy@OneToMany)的情况下是延迟加载的,所以在获取@manytomy@OneToMany关系对象时,您应该首先在DAO层中迭代,并以其他方式获取数据,它显示了懒散的初始化异常

牛昱
2023-03-14

这是不够的细节在您的问题以帮助您解决它的权利但你应该

  • 谷歌搜索LazyInitializationException,这是一个常见的新手问题。这是一个运行时异常,而不是编译时
  • 阅读您正在使用的框架(纯Spring或Spring Boot,JPA)的文档,了解如何打开事务/会话
  • 阅读JPA/Hibernate中的延迟初始化集合
  • 确保在视图中打开会话/事务(通常为反模式),或在代码上添加事务注释

查看您的代码,您正试图将JPA实体rihght序列化为JSON,这样视图中的开放会话将适合您,但您需要非常小心。此模式仅在非常简单的应用程序中有用。

 类似资料:
  • 我已经对这个错误进行了研究,但仍然找不到合适的解决方案。这是我的代码: 配置: 模型: 现在在我的控制器中,我调用导致异常的DAO的实现: 编辑: 这里是异常堆栈: 那么在这种情况下如何修复延迟加载异常呢? 编辑: 以下是DAO的实现:

  • 我不明白为什么会发生这种情况。根据代码路径,当引发此异常时,我应该在同一线程中并且会话应存在。 有人能告诉我我错过了什么吗? 我有设置 在hibernate.cfg.xml档案里 我在Servlet过滤器中创建了以下代码 在index.xhtml文件中,我有以下调用: 做一些事情 index.xhtml正在使用使用ui include加载menu.xhtml文件的模板。然后,菜单文件会插入menu

  • 问题内容: 我有下一个错误: 我的实体: 我有一个服务班: 我从另一种服务方法调用服务: 但是,当我试图调用这个方法我收到线异常,当我打电话时,也会发生同样的异常上。我已经检查了事务管理器,并且配置正确,并且此步骤中存在事务。另外,我已经检查了关于与此异常相关的stackoverflow的大量答案,但没什么用。 可能是什么原因造成的? 问题答案: 看来模型是一个独立的实体。 尝试合并并在合并实例上

  • 我有错误在我的Hibernate代码和我需要知道如何修复它 错误: “errorMessage:“无法初始化代理[tech.basarsoft.hayez.io.entity.University#gx8qcpipfnysrlripzmtgrt1qifuyb8]-无会话”, CollageEntryPOint类 拼贴: 和DAOSQL实现 这是hibernate。cfg文件 拼贴服务: 拼贴映射:

  • 我正在制作一个网站,我试图在其中订购配料。我选择配料并输入其数量,然后单击添加。我重复了几次。然后我进入下一页,在其中我选择了一个供应商。然后我按“订单”。 预计将Nabavka插入数据库并打开一个页面,说明订单成功,但我收到一个异常,告诉我不能懒惰地初始化它。 来自NabavkaController。java: Nabavka.java: 纳米尔尼卡。java: 纳巴夫卡科帕。java: 纳巴夫

  • 问题内容: 我有以下提到的实体类,当我执行我的应用程序时,我得到了以下异常。其他一些类似的问题也不能解决问题。 我该如何解决这个问题? Emp实体 部门实体 DAOImpl 泽西岛RESTful服务 springapplicationContext.xml 问题答案: 我已通过在web.xml中添加以下内容解决了该问题 在这里和这里礼貌 谢谢