I have a Spring project with EclipseLink as the JPA provider. I am able to successfully configure Lazy Loading in EclipseLink, but when I return the object, all the lazily loaded properties are triggered and fetched from the DB.

How do I prevent this behavior?

My classes are like below:

public class Request{
    ...properties...
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @PrimaryKeyJoinColumn
    private RequestDetail        requestDetail;
}

Main class

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

}

Log:

[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]

It is clear from the logs that the repo.getAll() method is not triggering lazy loading, but it is called after / during return process.

0 Answers