防止Spring响应触发EclipseLink延迟加载

时间:2019-04-23 05:34:03

标签: spring-boot spring-data-jpa eclipselink

我有一个使用EclipseLink作为JPA提供程序的Spring项目。我能够在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()方法不会触发延迟加载,但会在返回过程中/之后被调用。

0 个答案:

没有答案