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 assume that Hibernate Search will only index database entries that have been inserted using Hibernate. I tried to index the db manually using : -
fullTextEntityManager.createIndexer().startAndWait();
I have put this in a bean that runs after Spring initialises
public class SearchIndexer {
@Autowired
private EntityManagerFactory entityManagerFactory;
public SearchIndexer(){
}
@PostConstruct
public void doIndexing(){
EntityManager em = entityManagerFactory.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
try {
fullTextEntityManager.createIndexer().startAndWait();
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
I also Autowired it into my JUnit class and ran the doIndexing method manually (to be sure it was being picked up correctly AFTER the data was loaded).
@Before
public void setup() throws Exception{
dbUnitAdapter.setup("ClubDaoTest.xml");
searchIndexer.doIndexing();
super.before();
}
The dbUnitAdapter simply takes an XML file and inserts it in the db using DBUnit.
The entity is annotated like so: -
@Field
private String name;
@NotBlank
private String type;
@Field
private String address1;
@Field
private String county;
@Field
private String address2;
@Field
private String town;
@Field
private String country;
@Field
private String postcode;
private String telephone;
private String mobile;
private String fax;
@Field
private String email;
@Field
@NumericField
private long lft;
@Field
@NumericField
private long rgt;
I tried also tried inserting the data using hibernate (creating a Club entity), this didn't work either confusingly. I changed the the search index location from RAM to filesystem and used luke to read it. I could see the data that I'd tried inserting using hibernate, but no other data that I could see, although it was my first time using Luke, so I may have made a mistake.