-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate Search: index not updated on transaction rollback
PostPosted: Fri Feb 15, 2008 12:10 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi all,

I have setup Hibernate Search with Spring and Hibernate and am running some tests with it using JUnit.

At the end of a JUnit tests all the dummy data is removed from the database using a rollback. However, the data doesn't get removed from the Index. As a consequence, the Index keeps growing with every test. I understood that Hibernate was supposed to intercept CRUD activity and update the index accordingly. Perhaps I have an erronous config or I shouldn't execute the tx.commit() (then: how do I test the functionality):

Code:
applicationContext
   <bean
      class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

   <bean id="entityManagerFactory"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="loadTimeWeaver">
         <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
      </property>
      <property name="dataSource" ref="dataSource" />
      <property name="jpaVendorAdapter">
         <bean
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="database" value="MYSQL" />
            <property name="showSql" value="true" />
         </bean>
      </property>
   </bean>
   <bean id="transactionManager"
      class="org.springframework.orm.jpa.JpaTransactionManager">
      <property name="entityManagerFactory"
         ref="entityManagerFactory" />
   </bean>
   
   <tx:annotation-driven transaction-manager="transactionManager" />


Code:
persistence.xml
   <persistence-unit name="ApplicationEntityManager"
      transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <properties>
         <property name="hibernate.search.default.indexBase" value="d:/Java/Projects/indexes" />
         <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" />
      </properties>
   </persistence-unit>

Code:
POJO
@Entity
@Table(name="Offers")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorValue(value="OFFER")
@Indexed(index="offers")
public class Offer extends BaseObject implements IOffer{
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @DocumentId
   public Long getOfferId() {
      return offerId;
   }

   @Column
   @Field(store=Store.YES)
   public String getTitle() {
      return title;
   }

   @Column
   @Field(store=Store.YES)
   @Boost(2.5f)
   public String getDescription() {
      return description;
   }

[...]
}

Code:
searchDao
   public List<Object> findOffers(String keywords, boolean photosOnly, int start, int max)
         throws SearchException {
      List result=null;
      FullTextSession fullTextSession = Search
            .createFullTextSession(getHibernateTemplate()
                  .getSessionFactory().getCurrentSession());
      Transaction tx = fullTextSession.beginTransaction();

      MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[] {
            "title", "description" }, new StandardAnalyzer());
      try {
         Query query = parser.parse(keywords);

         org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(
               query, Offer.class);
         fullTextQuery.setFirstResult(start);
         fullTextQuery.setMaxResults(max);
         result = fullTextQuery.list();
         
      } catch (ParseException pe) {
         throw new SearchException(pe);
      }

      tx.commit();

      return result;
   }



My (AppFuse) project has the following structure:
core
src
web
indexes

On a side note, I'm finding that the index gets created in the specified directory indexes, but that when the individual items get added to the index, it saves them in core/offers. This seems erronous.

Kind regards,

Marc


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

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.