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