2

I want to load all objects from a table without a lazy objects/children and list them on the page (Thymeleaf template), but I get a LazyInitializationException every time. I tried to convert Hibernate entity objects into a POJO that doesnt contains a lazy/unwanted object but with the same result. I also tried open-in-view parameter set to false...

Simple example:

Parent:

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

Child:

@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();
    }
    ....
}

Controller:

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

EDIT: Thrown exception:

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: In DocumentDbe is relation with another object (EAGER this time so I was not paying attention to it) , which has reference to DocumentDbe again.. chained relationship and LazyInitializationException is created...

EDIT3: Although
This is modified and working controller, without 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";
}
mi0
  • 166
  • 9
  • Your One-to-one relation with DocumentFileDbe is marked as lazy. Either mark it as EAGER or use JPQL with eager fetch on the relations. – Shailendra Jul 21 '21 at 09:02
  • @Shailendra It is marked as lazy because I dont want to load it in this case. Thats the problem - I dont need it, POJO object doesnt have it, I am not filling Model with it but it throws LazyInitializationException anyway. – mi0 Jul 21 '21 at 09:37
  • Does this answer your question? [Could not initialize proxy - no Session](https://stackoverflow.com/questions/16752799/could-not-initialize-proxy-no-session) – Nikolai Shevchenko Jul 21 '21 at 10:46
  • Please provide the full stack trace. How do you pass `documentData` to `DocumentDto`? – v.ladynev Jul 21 '21 at 11:46
  • @v.ladynev Oh... I was pasting here whole stack trace and then I noticed the culprit. I had linked another object which has reference to DocumentDbe object too. And there I found my missing "LazyInitializationException" problem :( Sorry for unnecessary post/question... – mi0 Jul 21 '21 at 12:08

1 Answers1

0

In class DocumentDbe you have mark relation as Lazy. In default relation @ManyToOne and @OneToOne is as EAGER, so if you don't want Lazy, you have to change

@OneToOne(cascade = CascadeType.PERSIST)

If you want have @lob also as eager:

@Lob
@Basic( fetch = FetchType.EAGER )
Victor1125
  • 642
  • 5
  • 16
  • Thanks for the comment, but my issue is resolved. I wanted lazy relation, but DocumentDbe had another relation (EAGER this time) which has relation with DocumentDbe again... so that chain link was causing this trouble. – mi0 Jul 21 '21 at 12:10