*
* @throws Exception in case the test fails.
*/
public void testNoWorkDuplication() throws Exception {
FullTextSession s = org.hibernate.search.Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
// create new customer
SpecialPerson person = new SpecialPerson();
person.setName( "Joe Smith" );
EmailAddress emailAddress = new EmailAddress();
emailAddress.setAddress( "foo@foobar.com" );
emailAddress.setDefaultAddress(true);
person.addEmailAddress( emailAddress );
// persist the customer
s.persist( person );
tx.commit();
// search if the record made it into the index
tx = s.beginTransaction();
String searchQuery = "Joe";
QueryParser parser = new QueryParser( "Content", new StandardAnalyzer() );
Query luceneQuery = parser.parse( searchQuery );
FullTextQuery query = s.createFullTextQuery( luceneQuery );
List results = query.list();
assertTrue( "We should have a hit", results.size() == 1 );
tx.commit();
// Now try to delete
tx = s.beginTransaction();
int id = person.getId();
person = ( SpecialPerson ) s.get( SpecialPerson.class, id );
s.delete( person );
tx.commit();
// Search and the record via Lucene directly
tx = s.beginTransaction();
DirectoryProvider directoryProvider = s.getSearchFactory().getDirectoryProviders( SpecialPerson.class )[0];
ReaderProvider readerProvider = s.getSearchFactory().getReaderProvider();
IndexReader reader = readerProvider.openReader( directoryProvider );
IndexSearcher searcher = new IndexSearcher( reader );
try {
// we have to test using Lucene directly since query loaders will ignore hits for which there is no
// database entry
TopDocs topDocs = searcher.search( luceneQuery, null, 1 );
assertTrue( "We should have no hit", topDocs.totalHits == 0 );
}
finally {
readerProvider.closeReader( reader );
}
tx.commit();
s.close();
}