Lazy load handling (Hibernate + Spring MVC)

What is the best solution for handling lazy loadable objects in a Spring MVC application? I have already done some searches on this topic, and I have found the following solutions:

Open session in view: open a session for each request and close it after rendering the view. The problem with this solution is that I need lazy load objects also outside of the Spring MVC model (e.g. Junit example). Another issue discussed with this solution is exception handling. What if a transaction throws an exception while rendering a view?

An open session is explicit: open a session explicitly whenever I need a lazy loading of an object. Actually this solution should work, but I do not think it is right.

Using AOP: Creates an aspect that brings the lazy loading method to the session. It may be a solution, but I don’t know at what level of my application should I define poitcuts

Create custom queries: create queries for lazy loading and queries for active loading. This solution really works, but it seems wrong to use a lazy loading pattern

+4
source share
2 answers

, , , @Transactional .

, .

, , , .

- , , N + 1 - .

, , , , .

. post JBoss Seam OSIV (Seam , Hibernate).

, . , , OSIV - . Ocasional N + 1 .

, , , , .

. , (- angular.js) , , .

+2

, , "" , @Transactional Annotation. :

@Autowired
AccountDAO accountDAO; 

@Transactional
public List<String> getNamesOfAccount(String accountName){
    Account account = accountDAO.get(accountName);
    return account.getNames();
}

Account . !

0

Source: https://habr.com/ru/post/1523553/


All Articles