如何设置 Hibernate 的默认获取类型?
我有一个粗糙的旧 Java Enterprise 程序,我正试图将其引入当前十年,这要归功于安全审核将其导入的一半代码标记为安全风险。正如你们中的一些人所知,Hibernate 在 2010 年中期的某个时候将其默认设置从对所有内容的惰性加载更改为对几乎所有内容的急切加载。
问题是,这会严重破坏该程序。它是在假设所有内容都是延迟加载的情况下进行注释的,因此唯一的注释是针对它想要获取 EAGER 的内容,嗯,并不多。最终结果是连接数量大幅增加,这导致性能急剧下降,因为大多数连接实体并未在典型的批处理操作中使用。例如,为了按用户查询,用户字段是必需的。但是报表的用户已经在处理用户记录的循环顶部获取,因此向查询添加 JOIN 以急切地获取每条记录的用户只会使报表速度变慢。
我的大多数关系都没有针对延迟获取进行注释,而且它们的数量很多。我可以手动进入并费力地逐一注释它们以方便懒惰获取。或者我可以将 Hibernate 的默认值更改回编写该程序时的默认值。出于明显的原因,我显然更喜欢后者——我真的不想花更多的时间来更新这个古董代码库,因为我们正在编写它的替代品。
I have a crufty old Java Enterprise program that I'm trying to bring into the current decade thanks to security audits flagging half the code it imports as being a security risk. As some of you know, somewhere in the mid 2010's Hibernate changed its defaults from LAZY loading for everything, to EAGER loading for almost everything.
Problem is, this breaks this program bad. It was annotated assuming everything was lazy loading, so the only annotations are for things that it wanted to fetch EAGER, which, uhm, wasn't much. The end result is a huge increase in the number of joins, which has caused performance to plummet disastrously because most of the joined entities aren't used in a typical batch operation. For example, the User field is necessary in order to query by user. But the user for a report is already fetched at the top of the loop that processes the user's records, so adding a JOIN to the query to eager fetch the user for each and every record just makes the report slower.
Most of my relationships aren't annotated for lazy fetching, and there's a lot of them. I could manually go in and, laboriously, one by one, annotate them for lazy fetching. Or I could change Hibernate's defaults back to what they were back when this program was written. I'd obviously much prefer the latter, for obvious reasons -- I really don't want to be spending any more time updating this antique code base than I have to, since we're in the process of writing its replacement.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如此答案所指出的那样默认的获取类型,它将始终为:
,但是您可以使用简单的正则表达式自动添加懒惰的提取。在Intellij Idea中使用以下组合:CTRL+SHFT+R或CMD+SHFT+R,以及以下言论: @ManytoOne $或 @onetoone $,用于替换章节使用:@manytoone(@manytoone(fetch = fetchType.lazy.lazy)和@onetoone)和@onetoone (fetch = fetchtype.lazy)相应。
如果您已经在这些注释中添加了一些属性,则可以修改搜索正则是。无论如何,它会比手动进行更快
As pointed out in this answer https://stackoverflow.com/a/57586036/19351325 unfortunately there's no way to change the default fetch type, it will always be:
But you can use simple regex to automatically add Lazy fetching. In Intellij Idea use these combinations: ctrl+shft+r or cmd+shft+r, and the following regex: @ManyToOne$ or @OneToOne$ , for replace-section use: @ManyToOne(fetch = FetchType.LAZY) and @OneToOne(fetch = FetchType.LAZY) accordingly.
If you already have in these annotations some properties added you can just modify your search regex. Anyway it will be faster then doing it all manually
根据您在定义实体时提供的映射,有一个默认的获取计划。
There is a default fetch plan based on the mapping you provide while defining entities.