How to solve the hibernate N+1 problem?
In hibernate, one-to-one relationship causes the N+1 problem. for example a husband has a wife, and a wife has a husband. it's a typical example. go on the following scenario. if we query the specified husband list. hibernate querys it in this hql:
from Husband a [where ...]
then the problem appears. one husband in the list don't know his wife 'cause there's a foreign key in wife referring to her husband but not in husband referring to his wife. so hibernate does another query for each. something like:
from Wife b where b.husband=?
so we can clearly see that N+1 problem occurs. It's really an annoyed problem. how to solve it? it's hard to say. but fortunately some one does a lot of practice. there're two solutions. first , change the table structure. add a foregin key in husband table referring to his wife table. it makes a many-to-one relationship between husband and wife, our hbm may look like:
<many-to-one name="wife" column="WIFE_ID" not-null="false" cascade="all" class="mypackage.Wife" > </many-to-one>
but if it's not doable, we'd better get the second one, this solution don't change the table structure, and hibernate supplys formula which helps to define the fetch mode. the code below presents it:
<many-to-one name="wife" not-null="false" cascade="all" class="mypackage.Wife" > <formula>(select w.ID from WIFE w where w.HUSBAND_ID = ID)</formula> </many-to-one>
Note that the second one can also cause N+1 problem if we want to show the information of the husband and his wife in the same page. the configuration above can only tell hibernate how to get the related wife's id, so if we code as the following:
List <Husband> list = query.list(); list.get(0).getWife();
hibernate also does another query to the database. so I'm looking forward to your solutions and your share.
|
相关推荐
Polya is one of the most frequently quoted mathematicians, and the following statements from "How to Solve It" make clear why: "My method to overcome a difficulty is to go around it." "Geometry is ...
How to solve it Modern Huristics 2nd edition
How to solve it 清晰英文版
How To Solve Physics Problems How To Solve Physics Problems How To Solve Physics Problems
### 1. 教师的角色与学生独立工作的平衡 教师在帮助学生解决问题时,需找到一种微妙的平衡,既不能过度干涉,也不能完全放任。过度的帮助会剥夺学生自我探索的机会,而缺乏指导则可能导致学生陷入困境,无法前进。...
#### 1. 资源管理问题 资源管理是项目管理的核心之一,包括人力资源、财务资源、物资资源等。本书讨论了如何有效地规划、分配和监控资源,以确保项目目标的实现。解决资源管理问题的关键在于前期的充分规划,明确...
《如何解题,现代启发法》的英文版,DJVU 格式,可搜索。
作者: G. Polya 副标题: A New Aspect of Mathematical Method (Princeton Science Library) ISBN: 9780691119663 页数: 288 定价: USD 16.95 出版社: Princeton University Press 装帧: Paperback ...
[方法论]Polya How to Solve It 2004 英文版
How to solve 90% of NLP problems_ a step-by-step guide
1. 设定阶段(Set the Stage):在开始之前,需要确定一个需要解决的大问题,并组建一个团队。这个团队应由决策者(Decider)、协调者(Facilitator)以及来自不同领域和职能的成员组成。选择合适的工作时间和空间也...
How to Solve It:A New Aspect of Mathematical Method
Solve the following recurrence relation by repeated substitution T(n) = 2T(n/2) + n^3, T(1) = 1
Algorithm-How-to-Solve-it-by-Computer-R-G-Dromey.zip,如何用计算机求解-r.g.dromey解,算法是为计算机程序高效、彻底地完成任务而创建的一组详细的准则。
Practical Packet Analysis: Using Wireshark to Solve Real-World Network Problems by Chris Sanders English | 28 Mar. 2017 | ASIN: B06XX74R1X | 368 Pages | AZW3 | 24.73 MB Wireshark is the world's most ...
1. **定义域**:设定PDE的定义域,通常用x的范围表示。 2. **定义PDE**:写出偏微分方程的数学形式,注意将时间t和空间x作为独立变量。 3. **定义系数**:编写函数来返回PDE的系数,这个函数接受位置x和时间t作为...