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 ...
101 Project Management Problems and How to Solve Them
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
This is a physics dubject,it teaches you how to solve the problems in physics
Solve big problem and test new ideas in just 5 days
《如何解题,现代启发法》的英文版,DJVU 格式,可搜索。
how to solve it 怎样解题.
偏微分方程的Matlab解法,方便你使用MATLAB来解决偏微分问题!-Solution of Partial Differential Equations Matlab for you to use MATLAB to solve the partial differential problem!
作者: 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 it-some basic steps
How to solve 90% of NLP problems_ a step-by-step guide
engineering team at SugarCRM had built the application to solve this problem, yet few developers outside of SugarCRM really knew how powerful the underlying platform was. I knew there were other ...
How to Solve It_Modern Heuristics,启发式解决方法,欢迎下载
How to Solve Math Problems Using Symbolic Math Toolbox.zip
How to Solve It:A New Aspect of Mathematical Method