Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: JUnit/DBUnit testing Hibernate Search
PostPosted: Tue Aug 21, 2012 3:53 am 
Newbie

Joined: Thu Apr 05, 2012 6:19 am
Posts: 7
Hi Folks,

I am writing some JUnits for my hibernate search implementation.

I use a HSSQL in memory database. I use DBUnit to populate this DB (an XML file). It definitely works as other non-search tests work with the same data. The search code definitely works as I've tried it in the web-app and it returns the correct records.

I've tried a RAM index and a filesystem index.

I assume that Hibernate Search will only index database entries that have been inserted using Hibernate. I tried to index the db manually using : -
Code:
fullTextEntityManager.createIndexer().startAndWait();


This didn't help. I am not getting any results back at all.

Any ideas what I might be doing wrong?
Thanks for your help
All the best,
Ash


Top
 Profile  
 
 Post subject: Re: JUnit/DBUnit testing Hibernate Search
PostPosted: Tue Aug 21, 2012 5:08 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
http://stackoverflow.com/questions/12038189/hibernate-search-dbunit-indexing


Top
 Profile  
 
 Post subject: Re: JUnit/DBUnit testing Hibernate Search
PostPosted: Tue Aug 21, 2012 9:28 am 
Newbie

Joined: Thu Apr 05, 2012 6:19 am
Posts: 7
Hi Hardy,

Thanks for the reply. I've updated my question, hopefully it will give you some clues.

All the best,
Ash


Top
 Profile  
 
 Post subject: Re: JUnit/DBUnit testing Hibernate Search
PostPosted: Wed Aug 22, 2012 7:25 am 
Newbie

Joined: Thu Apr 05, 2012 6:19 am
Posts: 7
I have found the following output from Search: -

Code:
12/08/22 12:17:16 INFO impl.SimpleIndexingProgressMonitor: HSEARCH000027: Going to reindex 0 entities
12/08/22 12:17:16 INFO impl.SimpleIndexingProgressMonitor: HSEARCH000027: Going to reindex 0 entities
12/08/22 12:17:16 INFO impl.SimpleIndexingProgressMonitor: HSEARCH000028: Reindexed 0 entities
12/08/22 12:17:16 INFO impl.SimpleIndexingProgressMonitor: HSEARCH000027: Going to reindex 0 entities
12/08/22 12:17:16 INFO impl.SimpleIndexingProgressMonitor: HSEARCH000027: Going to reindex 0 entities
12/08/22 12:17:16 INFO impl.SimpleIndexingProgressMonitor: HSEARCH000028: Reindexed 0 entities


This suggests that the inserted data isn't found somehow (probably transaction related).

I tried @Transactional on the dbUnitAdapter.setup method in the hope that it would commit when the method is finished and therefore would be seen by the MassIndexer.

I also tried manually creating a transaction like so: -

Code:
@Before
  public void setup() throws Exception{
    TransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionStatus transStatus = transactionManager.getTransaction(definition);
    try {
      dbUnitAdapter.setup("ClubDaoTest.xml");
      transactionManager.commit(transStatus);
    }
    catch (Exception ex) {
        transactionManager.rollback(transStatus);
        throw ex;
    }
   
    searchIndexer.doIndexing();
    super.before();
  }


Alas, this didn't work either.

After I insert the data I can do hibernate queries on it successfully (there are other non-search tests in the JUnit class).

Here is my search code (this works, just not in unit tests)

Code:
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);

    QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder()
        .forEntity(Club.class).get();
   
    org.apache.lucene.search.Query query = qb.keyword()
        .onFields("name", "address1", "address2", "town", "county", "country", "attrValues.value")
        .matching(searchString).createQuery();
       
    BooleanJunction<BooleanJunction> b = qb.bool();
   
    b.must(query);
    b.must(qb.range().onField("lft").above(parentClub.getLft()).createQuery());
    b.must(qb.range().onField("lft").below(parentClub.getRgt()).createQuery());
   
   
    // wrap Lucene query in a javax.persistence.Query
    javax.persistence.Query persistenceQuery = fullTextEntityManager.createFullTextQuery(b.createQuery(),
        Club.class);
   
    persistenceQuery.setFirstResult((Integer) params.get(SQLQueryConstants.RECORD_INDEX))
                    .setMaxResults((Integer) params.get(SQLQueryConstants.OBJECTS_PER_PAGE));

   
    // execute search
    result = persistenceQuery.getResultList();


Thanks for your help!
All the best,
Ash


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.