У меня есть проект Spring с EclipseLink в качестве поставщика JPA. Я могу успешно настроить Lazy Loading в EclipseLink, но когда я возвращаю объект, все лениво загруженные свойства запускаются и извлекаются из БД.
Как мне предотвратить это поведение?
Мои занятия как ниже:
public class Request{
...properties...
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@PrimaryKeyJoinColumn
private RequestDetail requestDetail;
}
Основной класс
@SpringBootApplication
@RestController
public class JpaServiceApplication {
@Autowired
private RequestRepository repo;
public static void main(String[] args) {
SpringApplication.run(JpaServiceApplication.class, args);
}
@GetMapping(path = "/getAll")
public List<Request> getAllRequests(){
List<Request> requests= repo.getActiveRequests();
System.out.println("++++++++++++++++++++++CHECK LAZY LOADING+++++++++++++++++++++++++++");
return requests;
}
}
Вход:
[EL Fine]: sql: 2019-04-23 00:32:47.983--ServerSession(1586242955)--Connection(620381560) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT Query from Request table... where status=? and id <?
bind => [Closed, 50]
++++++++++++++++++++++CHECK LAZY LOADING+++++++++++++++++++++++++++
[EL Fine]: sql: 2019-04-23 00:32:48.353--ServerSession(1586242955)--Connection(2009214187)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [25]
[EL Fine]: sql: 2019-04-23 00:32:48.478--ServerSession(1586242955)--Connection(1188245631)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [24]
[EL Fine]: sql: 2019-04-23 00:32:48.605--ServerSession(1586242955)--Connection(713185885) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [23]
[EL Fine]: sql: 2019-04-23 00:32:48.729--ServerSession(1586242955)--Connection(1287753362)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [22]
[EL Fine]: sql: 2019-04-23 00:32:48.848--ServerSession(1586242955)--Connection(332271549) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [21]
[EL Fine]: sql: 2019-04-23 00:32:48.979--ServerSession(1586242955)--Connection(1659328877)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [20]
[EL Fine]: sql: 2019-04-23 00:32:49.096--ServerSession(1586242955)--Connection(1907365421)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [19]
[EL Fine]: sql: 2019-04-23 00:32:49.229--ServerSession(1586242955)--Connection(545346208) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [18]
[EL Fine]: sql: 2019-04-23 00:32:49.349--ServerSession(1586242955)--Connection(30059831) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [17]
[EL Fine]: sql: 2019-04-23 00:32:49.469--ServerSession(1586242955)--Connection(684643366) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [16]
[EL Fine]: sql: 2019-04-23 00:32:49.597--ServerSession(1586242955)--Connection(1443312033)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [15]
[EL Fine]: sql: 2019-04-23 00:32:49.721--ServerSession(1586242955)--Connection(2056726275)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [14]
[EL Fine]: sql: 2019-04-23 00:32:49.845--ServerSession(1586242955)--Connection(606512019) --Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [13]
[EL Fine]: sql: 2019-04-23 00:32:49.965--ServerSession(1586242955)--Connection(1300996592)--Thread(Thread[http-nio-8080-exec-1,5,main])--SELECT <Query from details table where id=?>
bind => [12]
Из журналов видно, что метод repo.getAll()
не вызывает отложенную загрузку, но вызывается после / во время процесса возврата.