Thanks Chris I appreciate your comments.
I have succesfully fixed the first changing the parameter to true and the second error implementing three interfaces in one class and adding some lines to hibernate.cfg.xml, below is the code in case somebody is interested:

****hibernate.cfg.xml**********

<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" />

*******************************


package com.estudiowebs.CMS.DAO;

import java.io.IOException;
import java.io.Serializable;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.hibernate.CallbackException;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PostInsertEvent;
import org.hibernate.event.PostInsertEventListener;
import org.hibernate.event.PostUpdateEvent;
import org.hibernate.event.PostUpdateEventListener;

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;
        }

}




Chris Hostetter wrote:
: following the interceptor method (the second one in
: http://www.hibernate.org/138.html)

I don't know much about hibernate, but I think i understand both of your
questions...

: 1. Lucene's doesn't create the needed folder structure the first time
: and it complains that it can't find segments.
:
: /Applications/OpenLaszlo Server
: 
3.1/Server/tomcat-5.0.24/webapps/com.estudiowebs.CMS/WEB-INF/luceneIndex/segments
: (No such file or directory)

If you look at the javadocs for IndexWriter, you'll note that the last
argument in the constructor indicates wether or not you want the
IndexWriter to make a new index, or open an existing index.  In the
URL you cited, the arg is allways false -- so unless you've got
seperate code in your application that uses an IndexWriter to create the
index, It's not suprising that the code in the article would generate an
error like that.

: 2. My database uses native ids which means that in the interceptor
: onSave() method the id is null and it throws an exception.

if you copied the onSave method exactly as it appears, then i'm not sure
how a null id could ever cause it to throw an exception ("null
instanceof Long" will be false, and the method should do nothing) but if
you read the comments at the bottom on the page, you'll see suggestions to
use something called a "PostInsertEventListener" instead.


...a side commentary on this article -- the hibernate side of things may
make perfect sense to someone who understands hibernate really well, but
as someone who (I like to think) understands the basics of Lucene very
well, I think it's very lacking in it's explanation of Lucene concepts and
API usage.  I would not send this URL to anyone as an example of
integrating Hibernate and Lucene unless I felt they already understood
Lucene thoroughly.  You may want to start by learning more about Lucene
independent of hibernate, reading though the demo application for example,
before trying to bridge the two...

        http://lucene.apache.org/java/docs/gettingstarted.html


-Hoss


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to