簡體   English   中英

Spring + Hibernate 沒有惰性 = LazyInitializationException

[英]Spring + Hibernate without lazy = LazyInitializationException

我想從沒有惰性對象/子項的表中加載所有對象並將它們列在頁面上(Thymeleaf 模板),但我每次都會收到 LazyInitializationException。 我嘗試將 Hibernate 實體對象轉換為不包含惰性/不需要的對象但結果相同的 POJO。 我還嘗試將open-in-view parameter設置為 false ...

簡單的例子:

家長:

@Entity
public class DocumentDbe implements Serializable {

    public DocumentDbe(){
    }
    
    @Id
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;
    
    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
    private DocumentFileDbe documentFile;
    ....
}

孩子:

@Entity
public class DocumentFileDbe implements Serializable {

    public DocumentFileDbe(){}
    
    @Id
    @Column(name = "id", updatable = false, nullable = false)
    private Long id;

    @Column
    @Lob
    private byte[] documentData;
    ...
 }

POJO:

public class DocumentDto implements Serializable {

    public DocumentDto(){
    }
    
    public DocumentDto(DocumentDbe doc){
        this.id = doc.getId();
    }
    ....
}

控制器:

@GetMapping("/list")
String getList(Model model) {
    List<DocumentDbe> docs;
    List<DocumentDto> data = new ArrayList<>();
    try (Session ses = sessionFactory.openSession()) {
        docs = ses.createQuery("FROM DocumentDbe").list();
        docs.forEach(doc -> {
            data.add(new DocumentDto(doc));
        });
    }
    model.addAttribute(MODEL_LIST_DATA, data);
    return "list";
}

編輯:拋出異常:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/list.html]")] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session

EDIT2:DocumentDbe是與另一個對象的關系(這次是 EAGER,所以我沒有注意它),它再次引用了DocumentDbe .. 鏈式關系和 LazyInitializationException 被創建...

EDIT3:雖然
這是修改后的工作控制器,沒有 POJO:

@GetMapping("/list")
String getList(Model model) {
    List<DocumentDbe> docs;
    try (Session ses = sessionFactory.openSession()) {
        docs = ses.createQuery("FROM DocumentDbe ORDER BY id DESC").list();
        docs.forEach(doc -> {
            doc.setDocumentFile(null);
            doc.getHistory().forEach(log ->{
                log.setDocument(null);
            });
        });
    }

    model.addAttribute(MODEL_ADMIN_DATA, docs);
    return "list";
}

在類DocumentDbe您將關系標記為 Lazy。 在默認關系中@ManyToOne@OneToOne是EAGER,所以如果你不想要Lazy,你必須改變

@OneToOne(cascade = CascadeType.PERSIST)

如果你想讓@lob 也同樣渴望:

@Lob
@Basic( fetch = FetchType.EAGER )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM