1. hiberante 一級緩存(session緩存)是沒有配置最大容量的,若是在一個事務中批量操做過多的對象,那麼極可能會出現OutOfMemoryError。正確的作法是每隔一段時間清空session緩存。如:java
Transaction tx = session.beginTransaction(); for (int i = 0; i < 1000000; i++) { session.save(user); if(i%25==0){ //每保存25條記錄就清空內存 session.flush(); session.clear(); } } tx.commit();
2.Hibernate.initialize方法能夠強制Hibernate當即加載關聯對象。如:sql
Hibernate.initialize(user.getAddress()); //加載賴加載對象 Hibernate.isInitialized(user.getAddress()); //判斷對象是否已經加載了
3. hibernate sql 查詢,返回實體緩存
String sql = "select u.id as {usr.id}, u.name as {usr.name},from T_User u"; String sql = "select {usr.*} from T_User usr "; List list = session.createSQLQuery(sql,"usr",TUser.class).list();