org.hibernate.search.test.indexingStrategy.ManualIndexingStrategyTest.java Source code

Java tutorial

Introduction

Here is the source code for org.hibernate.search.test.indexingStrategy.ManualIndexingStrategyTest.java

Source

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2010, Red Hat, Inc. and/or its affiliates or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat, Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.hibernate.search.test.indexingStrategy;

import org.apache.lucene.index.IndexReader;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.search.test.AlternateDocument;
import org.hibernate.search.test.Document;
import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.Environment;

/**
 * @author Emmanuel Bernard
 */
public class ManualIndexingStrategyTest extends SearchTestCase {

    public void testMultipleEntitiesPerIndex() throws Exception {

        Session s = getSessions().openSession();
        s.getTransaction().begin();
        Document document = new Document("Hibernate in Action", "Object/relational mapping with Hibernate",
                "blah blah blah");
        s.persist(document);
        s.flush();
        s.persist(new AlternateDocument(document.getId(), "Hibernate in Action",
                "Object/relational mapping with Hibernate", "blah blah blah"));
        s.getTransaction().commit();
        s.close();

        assertEquals(0, getDocumentNbr());

        s = getSessions().openSession();
        s.getTransaction().begin();
        s.delete(s.get(AlternateDocument.class, document.getId()));
        s.delete(s.createCriteria(Document.class).uniqueResult());
        s.getTransaction().commit();
        s.close();
    }

    private int getDocumentNbr() throws Exception {
        IndexReader reader = IndexReader.open(getDirectory(Document.class), false);
        try {
            return reader.numDocs();
        } finally {
            reader.close();
        }
    }

    protected Class<?>[] getAnnotatedClasses() {
        return new Class[] { Document.class, AlternateDocument.class };
    }

    protected void configure(Configuration cfg) {
        super.configure(cfg);
        cfg.setProperty(Environment.INDEXING_STRATEGY, "manual");
    }
}