码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate 1+N problem

时间:2015-10-27 08:14:40      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

What‘s 1+N problem?

When you are querying for one table, it auto calls sql command for each relational database if the fetch type is eager.In such case,it calls out 1+N sqls

For example:

class Thread: class Topic=1:M relationship, When we call select* from topic, it will also call select* from thread for each topic.thread_id

 

Solutions:

1. change FetchType of Topic to lazy, the sql command will only be called when neccesarry -- good when we don‘t need properties of relational DB

2. @BatchSize(size=) under @Entity in Thread class;--Not really solving the problem

  For example,size=4, then if we have 8 entries, it selects 4 ids each time, therefore, only 2 sql commands are called

3. Join Fetch.  session.createQuery("from Topic t left join fetch t.thread c") -- good when we do need properties of relational DB right afterwards

Hibernate 1+N problem

标签:

原文地址:http://www.cnblogs.com/fifi043/p/4913063.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!