@ManyToOne和@BatchSize [英] @ManyToOne and @BatchSize
问题描述
注释为 @ManyToOne $ c的字段$ c>也被注释为
@BatchSize
。
我一直认为 @BatchSize
注释仅在类层次或集合( @OneToMany
),并在迭代时影响预取。但是也许我错了,注释 @ManyToOne
与 @BatchSize
会影响某些内容。
使用注释
有意义吗? @ManyToOne
,我无法在文档中找到答案。 @BatchSize
@ManyToOne $ c $只有当相应的字段标记为
)。 lazy
()时,
> lazy = true
确实,如果该字段不是 lazy
,它的定义已经被加载,因为封装实体被加载,所以数据库调用的问题不适用。
想象一下 Person
ShoesPair 元素( ShoesPair
.class)的集合,并且在这个集合中存在一个
$ <一个人想穿过 25 双鞋(25 ShoesPair
物品)如果所有者
字段(对应于一个人)只用<$注释 c $ c> @ManyToOne ,会有 25 选择到数据库。然而,如果使用 @BatchSize(size = 5)
,只会有 5 调用,因此会提高性能。
从 Hibernate文档,但批量大小不仅适用于集合:
您也可也启用批量获取集合。
Hibenate特别提到 @OneToMany
个案,因为这些被应用于90%标记为 lazy
的案例中。
I found in some old code strange thing (at least for me).
The field which is annotated @ManyToOne
is also annotated with @BatchSize
.
I always thought that @BatchSize
annotation only affects when annotated at class level or on a collection (@OneToMany
) and affects pre-fetching when iterating.
But maybe I am wrong and annotating @ManyToOne
with @BatchSize
affects something. I can't find the answer in the documentation.
Does annotating @ManyToOne
with @BatchSize
have sense?
@ManyToOne
associated with @BatchSize
could make sense only if the corresponding field is marked as lazy
(lazy=true
).
Indeed, if the field is not lazy
, it's by definition already loaded since the enclosing entity is loaded, so the problem of database calls doesn't apply.
Imagine a Person
class who has a collection of ShoesPair
element (ShoesPair
.class) and within this one is present an owner
field marked as lazy (since optional and not really bringing an important information when retrieving a specific pair of shoes).
One wants to iterate through 25 pair of shoes (25 ShoesPair
objects) in order to retrieve their owner.
If the owner
field (corresponding to one person) is only annotated with @ManyToOne
, there would be 25 select to database.
However, if annoted with @BatchSize(size=5)
, there would be merely 5 calls and so increasing performance.
From the Hibernate documentation, it is precised that batch size does not only apply with collections:
You can also enable batch fetching of collections.
Hibenate mentions especially @OneToMany
cases, because these one are applied with fields that are in 90% of cases marked as lazy
.
这篇关于@ManyToOne和@BatchSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!