如何使 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章