/** Changes the parent LocationGroup's name to "Airport" */
  private void updateLocationGroupName(FullTextSession fullTextSession) {
    final Transaction transaction = fullTextSession.beginTransaction();

    LocationGroup group = (LocationGroup) fullTextSession.get(LocationGroup.class, 1L);
    LocationGroup locationGroup = (LocationGroup) fullTextSession.merge(group);
    locationGroup.setName("Airport");

    transaction.commit();
  }
  /** Adds a single Location to the LocationGroup#1 */
  private void addLocationToGroupCollection(FullTextSession fullTextSession) {
    final Transaction transaction = fullTextSession.beginTransaction();
    LocationGroup group = (LocationGroup) fullTextSession.get(LocationGroup.class, 1L);

    Location location = new Location("New Room");
    fullTextSession.persist(location);

    group.getLocations().add(location);
    location.setLocationGroup(group);

    transaction.commit();
  }
  @Test
  public void testContainedIn() throws Exception {
    FullTextSession s = Search.getFullTextSession(openSession());
    Transaction tx = s.beginTransaction();

    ProductCatalog productCatalog = new ProductCatalog();
    productCatalog.setName("Cars");
    Item item = new Item();
    item.setId(1);
    item.setDescription("test");
    item.setProductCatalog(productCatalog);
    productCatalog.addItem(item);

    s.persist(item);
    s.persist(productCatalog);
    tx.commit();

    s.clear();

    tx = s.beginTransaction();

    QueryParser parser = new QueryParser("id", TestConstants.standardAnalyzer);
    org.apache.lucene.search.Query luceneQuery = parser.parse("items.description:test");
    FullTextQuery query =
        s.createFullTextQuery(luceneQuery).setProjection(FullTextQuery.THIS, FullTextQuery.SCORE);
    assertEquals("expecting 1 results", 1, query.getResultSize());
    tx.commit();

    tx = s.beginTransaction();

    Item loaded = s.get(Item.class, item.getId());
    loaded.setDescription("Ferrari");

    s.update(loaded);
    tx.commit();

    tx = s.beginTransaction();

    parser = new QueryParser("id", TestConstants.standardAnalyzer);
    luceneQuery = parser.parse("items.description:test");
    query =
        s.createFullTextQuery(luceneQuery).setProjection(FullTextQuery.THIS, FullTextQuery.SCORE);
    assertEquals("expecting 0 results", 0, query.getResultSize());

    parser = new QueryParser("id", TestConstants.standardAnalyzer);
    luceneQuery = parser.parse("items.description:Ferrari");
    query =
        s.createFullTextQuery(luceneQuery).setProjection(FullTextQuery.THIS, FullTextQuery.SCORE);
    assertEquals("expecting 1 results", 1, query.getResultSize());
    tx.commit();

    tx = s.beginTransaction();
    @SuppressWarnings("unchecked")
    List<Object[]> results = query.list();

    for (Object[] result : results) {
      s.delete(result[0]);
    }
    tx.commit();
    s.close();
  }
Beispiel #4
0
  public void testTransactional() throws Exception {
    FullTextSession s = Search.getFullTextSession(openSession());
    Transaction tx = s.beginTransaction();
    final 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 = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    QueryParser parser =
        new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.stopAnalyzer);
    List result = s.createFullTextQuery(parser.parse("body:create")).list();
    assertEquals(0, result.size());
    tx.commit();
    s.close();

    s = Search.getFullTextSession(openSession());
    s.getTransaction().begin();
    s.doWork(
        new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
            Statement stmt = connection.createStatement();
            stmt.executeUpdate("update Email set body='Meet the guys who write the software'");
            stmt.close();
          }
        });
    s.doWork(
        new Work() {
          @Override
          public void execute(Connection connection) throws SQLException {
            // insert an object never indexed
            Statement stmt = 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 = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    parser =
        new QueryParser(TestConstants.getTargetLuceneVersion(), "id", TestConstants.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 = Search.getFullTextSession(openSession());
    tx = s.beginTransaction();
    result = s.createFullTextQuery(parser.parse("body:create")).list();
    assertEquals(1, result.size());
    tx.commit();
    s.close();
  }