Package org.hibernate.search.test.id

Source Code of org.hibernate.search.test.id.ProgrammaticEmbeddedItTest

/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.test.id;

import java.lang.annotation.ElementType;
import java.util.List;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;

import org.hibernate.Session;
import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;
import org.hibernate.search.cfg.Environment;
import org.hibernate.search.Search;
import org.hibernate.search.cfg.SearchMapping;
import org.hibernate.search.test.SearchTestBase;
import org.junit.Test;

import static org.junit.Assert.assertEquals;


/**
* Copied from EmbeddedIdTest, but using programmatic mapping instead.
*
* @author Emmanuel Bernard
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2012 Red Hat Inc.
*/
public class ProgrammaticEmbeddedItTest extends SearchTestBase {

  @Test
  public void testFieldBridge() throws Exception {
    PersonPK emmanuelPk = new PersonPK();
    emmanuelPk.setFirstName( "Emmanuel" );
    emmanuelPk.setLastName( "Bernard" );
    PlainPerson emmanuel = new PlainPerson();
    emmanuel.setFavoriteColor( "Blue" );
    emmanuel.setId( emmanuelPk );

    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.save( emmanuel );
    tx.commit();
    s.clear();

    tx = s.beginTransaction();
    List results = Search.getFullTextSession( s ).createFullTextQuery(
        new TermQuery( new Term( "id.lastName", "Bernard" ) )
    ).list();
    assertEquals( 1, results.size() );
    emmanuel = (PlainPerson) results.get( 0 );
    emmanuel.setFavoriteColor( "Red" );
    tx.commit();
    s.clear();

    tx = s.beginTransaction();
    results = Search.getFullTextSession( s ).createFullTextQuery(
        new TermQuery( new Term( "id.lastName", "Bernard" ) )
    ).list();
    assertEquals( 1, results.size() );
    emmanuel = (PlainPerson) results.get( 0 );
    assertEquals( "Red", emmanuel.getFavoriteColor() );
    s.delete( results.get( 0 ) );
    tx.commit();
    s.close();
  }

  @Override
  protected void configure(Configuration cfg) {
    super.configure( cfg );
    SearchMapping mapping = new SearchMapping();
    mapping
      .entity( PlainPerson.class )
        .indexed()
      .property( "id", ElementType.FIELD )
        .documentId()
        .bridge( PersonPKBridge.class )
      .property( "", ElementType.FIELD )
        .field();
    cfg.getProperties().put( Environment.MODEL_MAPPING, mapping );
  }

  @Override
  protected Class<?>[] getAnnotatedClasses() {
    return new Class[]{ PlainPerson.class };
  }

}
TOP

Related Classes of org.hibernate.search.test.id.ProgrammaticEmbeddedItTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.