Hibernate 1+N problem

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

posted @ 2015-10-27 07:26  fifi努刷题  阅读(177)  评论(0)    收藏  举报