Hibernate的兩個類設置了manyToOne(oneToMany)以後,在查詢的時候,因爲N 對1的一方默認的fetch=FetchType.EAGER,因此會把被關聯的對象一塊兒取出來 session
解決方法一:設置fetch=FetchType.LAZY,這種方法在合適的時候(具體使用到對象時)仍是會發出select語句。 fetch
解決方法二: spa
//List<Student> students= (List<Student>)session.createCriteria(Student.class).list();
List<Student> students= (List<Student>)session.createQuery("from Student").list(); 對象
也就是用session.createCriteria()作查詢,而不是用createQuery。 it
解決方法三:使用BatchSize(size=5)方法,他會發出1+N/5條select語句。 io
解決方法四:使用join fetch作外鏈接查詢。 class
from Topic t left join fetch t.category c List