-->
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.  [ 6 posts ] 
Author Message
 Post subject: Hibernate and Lucene onSave problem.
PostPosted: Tue Apr 05, 2005 8:06 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
Hi,

I want to index certain properties of my persistent beans using Lucene. I found the Wiki article that explains how to use an Interceptor to do so: http://www.hibernate.org/138.html. The problem is that the onSave method does not have the ID initialized. Is there an interceptor method that has the ID initialized when a new persistent object is being saved to the database?
The onDelete, onFlushDirty() would work fine. The problem is with the onSave method. How do I get around the interceptor onSave problem?

Hibernate version: 3.0

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 3:12 am 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
I have the exact same problem, I'm following the interceptor method with native generated ids wich makes the id to be null in the onSave method.

Any help is appreciated.

Raul.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 2:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check Hibernate Annotations, there is a Lucene integration through annotations and event listeners.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 10, 2005 6:13 pm 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
I followed the annotations approach and it didn't work for me even though I'm using JDK 5.0. Can I use annotations without using EJB?

I have temporarily solved the problem with a different approach:
Here is the code (not yet finished or properly tested) (The Searchable interface interface is the same as in the integration example):

Code:
<hibernate-configuration>
<session-factory>
   ...
   <!--  lucene indexer -->
   <listener type="post-update" class="com.estudiowebs.CMS.DAO.LuceneHibernateEventListener" />
   <listener type="post-insert" class="com.estudiowebs.CMS.DAO.LuceneHibernateEventListener" />
   <listener type="post-delete" class="com.estudiowebs.CMS.DAO.LuceneHibernateEventListener" />
  </session-factory>
</hibernate-configuration>


Code:
public class LuceneHibernateEventListener implements PostInsertEventListener,
      PostUpdateEventListener, PostDeleteEventListener {

   /**
    *
    */
   private static final long serialVersionUID = 1L;

   /**
    * Drop object from Lucene index
    */
   public void drop(Searchable entity, Long id) throws IOException {

      System.out.println("Drop in index called for:" + entity);
      IndexReader reader = entity.getIndexReader();
      reader.delete(new Term("id", String.valueOf(id)));
   }

   /**
    * Add object to Lucene index
    */
   public void add(Searchable entity, Long id) throws IOException {

      System.out.println("Add to index called for:" + entity);

      Document doc = entity.getDocument();
      doc.add(Field.Keyword("id", String.valueOf(id)));
      doc.add(Field.Keyword("classname", entity.getClass().getName()));
      IndexWriter writer = entity.getIndexWriter();
      
      writer.addDocument(doc);
      writer.close();

   }

   public void onPostInsert(PostInsertEvent event) {

      System.out.println("LuceneInterceptor onSave called for: "
            + event.getEntity() + " id " + event.getId());

      if (event.getEntity() instanceof Searchable) {

         try {
            add((Searchable) event.getEntity(),
                  resolveToLong(event.getId()));
         } catch (IOException e) {
            throw new CallbackException(e.getMessage());
         }

      }

   }

   public void onPostUpdate(PostUpdateEvent event) {
      System.out.println("onFlushDirty called for:" + event.getEntity()
            + " id " + event.getId());

      if (event.getEntity() instanceof Searchable) {

         try {
            drop((Searchable) event.getEntity(), resolveToLong(event
                  .getId()));
            add((Searchable) event.getEntity(),
                  resolveToLong(event.getId()));
         } catch (IOException e) {
            throw new CallbackException(e.getMessage());
         }

      }

   }

   public void onPostDelete(PostDeleteEvent event) {

      if (event.getEntity() instanceof Searchable) {

         try {
            drop((Searchable) event.getEntity(), resolveToLong(event
                  .getId()));
         } catch (IOException e) {
            throw new CallbackException(e.getMessage());
         }

      }

   }

   private Long resolveToLong(Serializable id) {
      if (id instanceof Integer) {
         return new Long((Integer) id);
      } else if (id instanceof String) {
         return Long.valueOf((String) id);
      } else if (id instanceof Long) {
         return (Long) id;
      }

      return null;
   }

}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 11, 2005 1:10 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
dobleerre wrote:
I followed the annotations approach and it didn't work for me even though I'm using JDK 5.0. Can I use annotations without using EJB?


Sure, you can use Hibernate annotations wo EJBs

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 1:00 am 
Beginner
Beginner

Joined: Mon Feb 07, 2005 10:40 pm
Posts: 22
Thanks for the info, I'll try it


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.