hibernate学习笔记第19讲-一级缓存

 

hibernate一级缓存   

一级缓存很短和session的生命周期一致,一级缓存也叫session级的缓存或事务级缓存

 

那些方法支持一级缓存:

     * get()

     * load()

     * iterate(查询实体对象)

    

如何管理一级缓存:

     * session.clear(),session.evict()

    

如何避免一次性大量的实体数据入库导致内存溢出

     * flush,再clear

    

如果数据量特别大,考虑采用jdbc实现,如果jdbc也不能满足要求可以考虑采用数据本身的特定导入工具      

     /**

      * 在同一个session中发出两次load查询

      */

session.beginTransaction();

                       Student student = (Student)session.load(Student.class, 1);

                       System.out.println("student.name=" + student.getName());

                       //不会发出sql,因为load使用缓存

                       student = (Student)session.load(Student.class, 1);

                       System.out.println("student.name=" + student.getName());

                       session.getTransaction().commit();

/**

      * 在同一个session中发出两次get查询

      */

session.beginTransaction();

                       Student student = (Student)session.get(Student.class, 1);

                       System.out.println("student.name=" + student.getName());

                       //不会发出sql,因为get使用缓存

                       student = (Student)session.get(Student.class, 1);

                       System.out.println("student.name=" + student.getName());

                       session.getTransaction().commit();

/**

      * 在同一个session中发出两次iterate查询实体对象

      */

                        session.beginTransaction();

                       Student student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();

                       System.out.println("student.name=" + student.getName());

                       //会发出查询idsql,不会发出查询实体对象的sql,因为iterate使用缓存

                       student = (Student)session.createQuery("from Student s where s.id=1").iterate().next();

                       System.out.println("student.name=" + student.getName());

                      

                       session.getTransaction().commit();

     /**

      * 在同一个session中发出两次iterate查询普通属性

      */

session.beginTransaction();

                       String name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();

                       System.out.println("student.name=" + name);

                       //iterate查询普通属性,一级缓存不会缓存,所以发出sql

                       //一级缓存是缓存实体对象的

                       name = (String)session.createQuery("select s.name from Student s where s.id=1").iterate().next();

                       System.out.println("student.name=" + name);

                       session.getTransaction().commit();

     /**

      * 开启两个session中发出load查询

      */

//会发出查询语句,session间不能共享一级缓存的数据

//因为它会伴随session的生命周期存在和消亡

     /**

      * 在同一个session中先save,在发出load查询save过的数据

      */

session.beginTransaction();

                       Student stu = new Student();

                       stu.setName("王五");

                       Serializable id = session.save(stu);

                       //不会发出sql,因为save是使用缓存的

                       Student student = (Student)session.load(Student.class, id);

                       System.out.println("student.name=" + student.getName());

                       session.getTransaction().commit();

     /**

      * 向数据库中批量加入1000条数据

      */

session.beginTransaction();

                       for (int i=0; i<1000; i++) {

                                 Student student = new Student();

                                 student.setName("s_" + i);

                                 session.save(student);

                                 //20条数据就强制session将数据持久化

                                 //同时清除缓存,避免大量数据造成内存溢出

                                 if ( i % 20 == 0) {

                                          session.flush();

                                          session.clear();

                                 }

                       }

                       session.getTransaction().commit();

你可能感兴趣的:(java,Hibernate,jdbc,领域模型)