s.close();
}
public void testTransactional() throws Exception {
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
int loop = 4;
for (int i = 0; i < loop; i++) {
Email email = new Email();
email.setId( (long) i + 1 );
email.setTitle( "JBoss World Berlin" );
email.setBody( "Meet the guys who wrote the software" );
s.persist( email );
}
tx.commit();
s.close();
//check non created object does get found!!1
s = new FullTextSessionImpl( openSession() );
tx = s.beginTransaction();
QueryParser parser = new QueryParser( "id", new StopAnalyzer() );
List result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
assertEquals( 0, result.size() );
tx.commit();
s.close();
s = new FullTextSessionImpl( openSession() );
s.getTransaction().begin();
Statement stmt = s.connection().createStatement();
stmt.executeUpdate( "update Email set body='Meet the guys who write the software'" );
stmt.close();
//insert an object never indexed
stmt = s.connection().createStatement();
stmt.executeUpdate( "insert into Email(id, title, body, header) values( + "
+ ( loop + 1 ) + ", 'Bob Sponge', 'Meet the guys who create the software', 'nope')" );
stmt.close();
s.getTransaction().commit();
s.close();
s = new FullTextSessionImpl( openSession() );
tx = s.beginTransaction();
parser = new QueryParser( "id", new StopAnalyzer() );
result = s.createFullTextQuery( parser.parse( "body:write" ) ).list();
assertEquals( 0, result.size() );
result = s.createCriteria( Email.class ).list();
for (int i = 0; i < loop / 2; i++)
s.index( result.get( i ) );
tx.commit(); //do the process
s.index( result.get( loop / 2 ) ); //do the process out of tx
tx = s.beginTransaction();
for (int i = loop / 2 + 1; i < loop; i++)
s.index( result.get( i ) );
tx.commit(); //do the process
s.close();
s = Search.getFullTextSession( openSession() );
tx = s.beginTransaction();
//object never indexed
Email email = (Email) s.get( Email.class, Long.valueOf( loop + 1 ) );
s.index( email );
tx.commit();
s.close();
//check non indexed object get indexed by s.index
s = new FullTextSessionImpl( openSession() );
tx = s.beginTransaction();
result = s.createFullTextQuery( parser.parse( "body:create" ) ).list();
assertEquals( 1, result.size() );
tx.commit();
s.close();
}