-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Optimized mass indexing using JPA
PostPosted: Mon Apr 13, 2009 4:28 am 
Beginner
Beginner

Joined: Tue Feb 03, 2009 12:29 pm
Posts: 49
hi,

I looked at the documentation on ways to perform optimized batch indexing. However, the example uses a scrollable result set and Critera API which do not seem to have equivalents in JPA. According to my understanding, in JPA we have to use pagination to avoid the OutOfMemoryError. So I wrote the following code. Can someone confirm any issues with this approach or suggest a better approach?
Thanks
Seema

Code:
EntityManager em = entityManagerFactory.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
em.getTransaction().begin();
fullTextEntityManager.purgeAll();

int maxRecords = 1000;
int startPosition = 0;

while(true) {
   Query selectQuery = em.createQuery("select book from Book as book");
   selectQuery.setMaxResults(maxRecords);
   selectQuery.setFirstResult(startPosition);
   List books = selectQuery.getResultList();
   if (books.isEmpty()){
         break;
   }
   for (Book book : books) {
          fullTextEntityManager.index(book);
   }
   fullTextEntityManager.flushToIndexes();
   em.clear();
   startPosition = startPosition + books.size();
}
fullTextEntityManager.getSearchFactory().optimize(Book.class);
em.getTransaction().commit();
em.close();



Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 13, 2009 5:35 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
your code looks fine, if you prefer the scrollableresult you could use
Code:
Session session = (Session) em.getDelegate()

so you can use the criteria API.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.