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

hibernate一级缓存   

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

 

那些方法支持一级缓存:

     * get()

     * load()

     * iterate(查询实体对象)

    

如何管理一级缓存:

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

    

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

     * 先flush,再clear

    

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

     /**

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

      */

session.beginTransaction();

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

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

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

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

/**

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

      */

session.beginTransaction();

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

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

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

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

/**

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

      */

                        session.beginTransaction();

                       Studentstudent = (Student)session.createQuery("from Student s wheres.id=1").iterate().next();

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

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

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

                       session.getTransaction().commit();

     /**

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

      */

session.beginTransaction();

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

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

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

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

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

     /**

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

      */

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

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

     /**

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

      */

session.beginTransaction();

                       Studentstu = new Student();

                       stu.setName("王五");

                       Serializableid = session.save(stu);

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

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

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

     /**

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

      */

session.beginTransaction();

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

                                 Studentstudent = new Student();

                                 student.setName("s_"+ i);

                                 session.save(student);

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

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

                                 if( i % 20 == 0) {

                                          session.flush();

                                          session.clear();

                                 }

                       }

                       session.getTransaction().commit();

相关推荐