Aller au contenu

Open Session In View (ARCH4067)

Ce contenu n’est pas encore disponible dans votre langue.

Key java.orm.open-session-in-view · Severity warning · Category orm

Dereferencing lazy associations in the view/serialization layer (Open Session in View) keeps the DB session open and produces hidden N+1. Load the data in the service layer inside a transaction.

@GetMapping("/orders")
public List<Order> list() {
return orderRepository.findAll(); // lazy items -> loaded during JSON serialization
}
// spring.jpa.open-in-view=true
@GetMapping("/orders")
public List<OrderDto> list() {
return orderService.findAllWithItems(); // fetch inside transaction + DTO
}
// spring.jpa.open-in-view=false

Disable open-in-view; load lazy data in the service/transaction layer and return DTOs.