@ManyToOne和@BatchSize [英] @ManyToOne and @BatchSize

查看:147
本文介绍了@ManyToOne和@BatchSize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



注释为 @ManyToOne 也被注释为 @BatchSize



我一直认为 @BatchSize 注释仅在类层次或集合( @OneToMany ),并在迭代时影响预取但是也许我错了,注释 @ManyToOne @BatchSize 会影响某些内容。

使用注释 @ManyToOne ,我无法在文档中找到答案。 @BatchSize 有意义吗?

解决方案

@ManyToOne lazy )时, @BatchSize > 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆