-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Search and Spring
PostPosted: Sun Jul 19, 2009 5:11 am 
Newbie

Joined: Sun Jul 19, 2009 4:47 am
Posts: 5
Welcome!

I have searched a lot (Google and this discussion board, https://www.hibernate.org/441.html), tried lots of old hibernate and spring versions, and still I cannot make it work correctly. Everything seems to work fine except reindexing on update relation entity. I think it might be an issue associated with executing FullTextIndexEventListener.

My example is the authors<->books application. I do the following in my index test case:
Code:
        AuthorDAO authorDAO = (AuthorDAO) ctx.getBean("authorDAOImpl");
        AuthorEntity a = new AuthorEntity();
        a.setFirstName("author");
        a.setLastName("author");
        authorDAO.saveAuthor(a);

        BookDAO bookDAO = (BookDAO) ctx.getBean("bookDAOImpl");
        BookEntity b = new BookEntity();
        b.setTitle("title");
        b.setPublicationDate(new Date());
        b.setContent("content");
        Set<AuthorEntity> authors = new HashSet<AuthorEntity>();
        authors.add(a);
        b.setAuthors(authors);
        bookDAO.saveBook(b);

// Everything ok here. Indexed data is correct.

        a.setFirstName("Changed"); // Changing value.
        authorDAO.saveAuthor(a); // Here the authors.firstName index property is not being reindexed


Library versions:
Hibernate 3.3.2.GA
Hibernate Search 3.1.1.GA
Hibernate Annotations 3.4.0.GA
Spring 2.5.6.SEC01

My code:
Spring configuration:
Code:
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:jdbc.properties"/>
    </bean>

    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"
          init-method="createDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <bean id="hibernateSessionFactory"
          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="schemaUpdate" value="true"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect = org.hibernate.dialect.DataDirectOracle9Dialect
                hibernate.hbm2ddl.auto = create
                hibernate.show_sql = true
                hibernate.format_sql = false
                hibernate.search.default.directory_provider = org.hibernate.search.store.FSDirectoryProvider
                hibernate.search.default.indexBase = C:\index\
            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>edu.lantoniak.hibernate.search.model.AuthorEntity</value>
                <value>edu.lantoniak.hibernate.search.model.BookEntity</value>
            </list>
        </property>
    </bean>

    <bean id="authorDAOImpl" class="edu.lantoniak.hibernate.search.dao.AuthorDAOImpl">
        <property name="sessionFactory" ref="hibernateSessionFactory"/>
    </bean>

    <bean id="bookDAOImpl" class="edu.lantoniak.hibernate.search.dao.BookDAOImpl">
        <property name="sessionFactory" ref="hibernateSessionFactory"/>
    </bean>


DAO implementation:
Code:
public class BookDAOImpl extends HibernateDaoSupport implements BookDAO {
    public void saveBook(BookEntity b) {
        getHibernateTemplate().saveOrUpdate(b);
    }
}


Mappings:
Code:
@Entity
@javax.persistence.SequenceGenerator(name="BOOKS_SEQ_GEN", sequenceName="BOOKS_SEQ")
@Table(name="BOOKS")
@Indexed
public class BookEntity {
...
    @ManyToMany(targetEntity=AuthorEntity.class)
    @JoinTable(name="AUTHORS_BOOKS", joinColumns=@JoinColumn(name="BOOKS_ID"), inverseJoinColumns=@JoinColumn(name="AUTHORS_ID"))
    @IndexedEmbedded(depth=1)
    public Set<AuthorEntity> getAuthors() {
        return authors;
    }
...
}

Code:
@Entity
@javax.persistence.SequenceGenerator(name="AUTHORS_SEQ_GEN", sequenceName="AUTHORS_SEQ")
@Table(name="AUTHORS")
@Indexed
public class AuthorEntity {
...
    @ManyToMany(mappedBy="authors", targetEntity=BookEntity.class)
    @ContainedIn
    public Set<BookEntity> getBooks() {
        return books;
    }
...
}


I have tried getHibernateTemplate().setExposeNativeSession(true); before savOrUpdate, and explicite registering eventListners but it did not help.
I will be very grateful for any help.

Regards,
Lukasz

Edit: I have tried also playing with:
Code:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="hibernateSessionFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    @Transactional(propagation=Propagation.REQUIRED)
    public void saveBook(BookEntity b) {
        getHibernateTemplate().saveOrUpdate(b);
    }

But still nothing happens.


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Spring
PostPosted: Sun Jul 19, 2009 9:58 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
BTW have you committed (or called flush(); flushToIndexes(); ) before doing your test on the index?
Changes are only visible after these boundaries.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Spring
PostPosted: Sun Jul 19, 2009 10:42 am 
Newbie

Joined: Sun Jul 19, 2009 4:47 am
Posts: 5
Actually HibernateTemplate shall do it for me. I have changed the implementation of saveAuthor() method to one shown below, but still I do not see "Changed" first name.

Code:
public void saveAuthor(AuthorEntity a) {
        Session session = getSession();
        FullTextSession fullTextSession = Search.getFullTextSession(session);
        Transaction t = fullTextSession.beginTransaction();
        fullTextSession.saveOrUpdate(a);
        t.commit();
        fullTextSession.flush();
        fullTextSession.flushToIndexes();
}


In the database everything is ok. (I see "Changed" first names)

Edit:
I would say even more (the SessionFactory received from HibernateTemplate has event listeners present):
Code:
@Transactional(propagation=Propagation.REQUIRED)
    public void saveAuthor(AuthorEntity a) {
        getHibernateTemplate().saveOrUpdate(a);
        EventListeners e = ((SessionFactoryImpl)getHibernateTemplate().getSessionFactory()).getEventListeners();
        System.out.println(e.getPostCollectionRecreateEventListeners()[0].getClass().getName());
        System.out.println(e.getPostCollectionRemoveEventListeners()[0].getClass().getName());
        System.out.println(e.getPostCollectionUpdateEventListeners()[0].getClass().getName());
    }

Returns:
Code:
org.hibernate.search.event.FullTextIndexEventListener
org.hibernate.search.event.FullTextIndexEventListener
org.hibernate.search.event.FullTextIndexEventListener


Top
 Profile  
 
 Post subject: Re: Hibernate Search and Spring
PostPosted: Mon Jul 20, 2009 12:42 pm 
Newbie

Joined: Sun Jul 19, 2009 4:47 am
Posts: 5
Issue solved. I have been using to up-to-date author object. Sorry for making trouble, and thank you for your help.


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