JPA default fetch type

后端 未结 4 760
小鲜肉
小鲜肉 2021-02-07 03:14

From my understanding @OneToOne and @ManyToOne JPA annotations do an eager fetch. I want these to be lazily loaded in my application, or a

4条回答
  •  情深已故
    2021-02-07 03:53

    no way. there is nothing about changing global fetching strategy in JPA specification. JPA offers for 1-1/N-1 associations EAGER fetching, whereas for 1-N/M-N it is LAZY. All JPA implementations should follow the specification to be compliant. I think, it is better that the application developer can't change this default beheavior globally, since these are the best practices in almost most cases unless you've just one type of association between all entities, like 1-1. think about, that you could set it to "EAGER" in an application, which contains a really rich entity model with complex relations and millions of data in the database. Overriding the fetching strategy per association manually allows the developer to take the responsibility about what will next happen. it is not error prone, instead, a powerful feature.

提交回复
热议问题