如何使 JPA OneToOne 关系变得懒惰 - How can I make a JPA OneToOne relation lazy

问题:

In this application we are developing, we noticed that a view was particularly slow.在我们正在开发的这个应用程序中,我们注意到一个视图特别慢。 I profiled the view and noticed that there was one query executed by hibernate which took 10 seconds even if there only were two object in the database to fetch.我分析了该视图,并注意到 hibernate 执行了一个查询,即使数据库中只有两个对象要获取,该查询也需要 10 秒。 All OneToMany and ManyToMany relations were lazy so that wasn't the problem.所有OneToManyManyToMany关系都是懒惰的,所以这不是问题。 When inspecting the actual SQL being executed, I noticed that there were over 80 joins in the query.在检查正在执行的实际 SQL 时,我注意到查询中有 80 多个连接。

Further inspecting the issue, I noticed that the problem was caused by the deep hierarchy of OneToOne and ManyToOne relations between entity classes.进一步检查问题,我注意到问题是由实体类之间的OneToOneManyToOne关系的深层层次结构引起的。 So, I thought, I'll just make them fetched lazy, that should solve the problem.所以,我想,我会让他们偷懒,这应该可以解决问题。 But annotating either @OneToOne(fetch=FetchType.LAZY) or @ManyToOne(fetch=FetchType.LAZY) doesn't seem to work.但是注释@OneToOne(fetch=FetchType.LAZY)@ManyToOne(fetch=FetchType.LAZY)似乎不起作用。 Either I get an exception or then they are not actually replaced with a proxy object and thus being lazy.要么我得到一个异常,要么它们实际上并没有被代理对象替换,因此很懒惰。

Any ideas how I'll get this to work?任何想法我将如何让它发挥作用? Note that I do not use the persistence.xml to define relations or configuration details, everything is done in java code.请注意,我没有使用persistence.xml来定义关系或配置细节,一切都是在java 代码中完成的。


解决方案:

参考一: https://stackoom.com/question/63hz
参考二: How can I make a JPA OneToOne relation lazy
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章