廣告

2016年5月19日 星期四

Hibernate batch process


 //in pojo (java bean)
      @BatchSize(size=5)
------------------------------------------------
      public void insertOldPost2HotPost() throws Exception{
       
       Session session = sessionFactory.getCurrentSession();

        @SuppressWarnings("unchecked")
        List posts = session.createQuery("from Post ").list();
        int itCount=0;
        for(Iterator it = posts.iterator(); it.hasNext(); ) {

            itCount++;

            Post post = (Post) it.next();

            HotPost hotPost = new HotPost(post);
            session.saveOrUpdate(hotPost);

            if (itCount % 5 == 0) {
            
                session.flush();
                session.clear();
            }
        }
    }


ref:


沒有留言:

張貼留言