Hibernate Amongst Infinispan Tutorial For Beginners

This tutorial is for developers who are only starting fourth dimension to acquire hibernate-search in addition to trying to gear upwards a demo project. Most of the codes are copied from the hibernate-search documentation: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/

What nosotros need:

  • eclipse-neon
  • hibernate-search 5.7.0
  • wildfly 10.1.0.Final
  • infinispan 8.2.6 (infinispan-as-embedded-modules-8.2.6.Final.zip), download this from the infinispan website in addition to extract inward your wildfly modules folder - create non forget to create this otherwise you'll stimulate got a missing degree exception
Note:
I created my projection using javaee7 maven archetype.

Since hibernate-search has already explained a lot nosotros volition only accept banking concern complaint of the near of import pieces:
  1. persistence.xml
     <property name="hibernate.show_sql" value="true" /> <!-- Enables instant flat cache, telephone telephone uncovering afterward insert in addition to no inquiry inward the database volition endure number --> <property name="hibernate.cache.use_second_level_cache" value="false"/> <!-- Enable inquiry cache --> <property name="hibernate.cache.use_query_cache" value="false"/> <!-- optional --> <property name="hibernate.search.default.directory_provider"  value="infinispan" /> <property name="hibernate.search.infinispan.cache_jndiname"  value="java:jboss/infinispan/broodcampHibernateSearch" /> <!-- <property name="hibernate.search.default.directory_provider" --> <!-- value="filesystem" /> --> <property name="hibernate.search.default.indexBase" value="c:/temp/lucene/indexes" />

    In hither nosotros laid the directory_provider to infinispan, thus nosotros too laid the cache JNDI name. So inward wildfly's standalone.xml, nosotros must add together the cache below:
    <cache-container name="broodcamp" default-cache="broodcamp-hibernate-search">  <local-cache name="broodcamp-hibernate-search" jndi-name="java:jboss/infinispan/broodcampHibernateSearch">   <file-store path="c:/temp/lucene/indexes" passivation="true" purge="false"/>  </local-cache> </cache-container>
  2. Create a degree Author in addition to Book from hibernate search documentation.
  3. In our pom nosotros must define the next dependencies:
    <dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-search-orm</artifactId> </dependency>  <dependency>  <groupId>org.infinispan</groupId>  <artifactId>infinispan-directory-provider</artifactId>  <version>8.2.4.Final</version>  <scope>provided</scope> </dependency> 
  4. Now let's create an arquillian test:
    1. arquillian.xml
      <?xml version="1.0" encoding="UTF-8"?> <arquillian xmlns="http://jboss.org/schema/arquillian"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://jboss.org/schema/arquillian         http://jboss.org/schema/arquillian/arquillian_1_0.xsd">   <defaultProtocol type="Servlet 3.0" />   <container qualifier="wildfly" default="true">   <configuration>    <property name="jbossHome">C:\Java\jboss\wildfly-10.1.0.Final</property>   </configuration>  </container>   <engine>   <property name="deploymentExportPath">target/deployments</property>  </engine>  </arquillian> 
      Set jbossHome to the right value.
    2. Create our arquillian exam class:
      /*  * JBoss, Home of Professional Open Source  * Copyright 2013, Red Hat, Inc. and/or its affiliates, in addition to private  * contributors yesteryear the @authors tag. See the copyright.txt inward the  * distribution for a sum listing of private contributors.  *  * Licensed nether the Apache License, Version 2.0 (the "License");  * you lot may non role this file except inward compliance alongside the License.  * You may obtain a re-create of the License at  * http://www.apache.org/licenses/LICENSE-2.0  * Unless required yesteryear applicable constabulary or agreed to inward writing, software  * distributed nether the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either limited or implied.  * See the License for the specific linguistic communication governing permissions in addition to  * limitations nether the License.  */ bundle com.broodcamp.hibernatesearch;  import static org.junit.Assert.assertEquals;  import java.util.List; import java.util.logging.Logger;  import javax.inject.Inject; import javax.persistence.EntityManager;  import org.hibernate.search.jpa.FullTextEntityManager; import org.hibernate.search.jpa.Search; import org.hibernate.search.query.dsl.QueryBuilder; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Test; import org.junit.runner.RunWith;  @RunWith(Arquillian.class) world degree HibernateSearchTest {   private Logger log = Logger.getLogger(this.getClass().getName());   @Inject  private EntityManager em;   @Deployment  world static Archive<?> createTestArchive() {   render ShrinkWrap.create(WebArchive.class, "test.war")     .addClasses(Author.class, Book.class, Resources.class, StartupListener.class)     .addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml")     .addAsResource("import.sql", "import.sql").addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")     // Deploy our exam datasource     .addAsWebInfResource("test-ds.xml", "test-ds.xml");  }   @SuppressWarnings("unchecked")  @Test  world void testSimpleJPALuceneSearch() {   log.info("testSimpleJPALuceneSearch");    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);    // create native Lucene inquiry using the inquiry DSL   // alternatively you lot tin write the Lucene inquiry using the Lucene inquiry   // parser   // or the Lucene programmatic API. The Hibernate Search DSL is   // recommended though   QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get();   org.apache.lucene.search.Query luceneQuery = qb.keyword().onFields("title", "subTitle", "authors.name")     .matching("Programmers").createQuery();    // twine Lucene inquiry inward a javax.persistence.Query   javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Book.class);    // execute search   List<Book> outcome = (List<Book>) jpaQuery.getResultList();    log.info("Record found=" + result.size());   result.forEach(p -> log.info(p.toString()));    assertEquals(9, result.size());  }   @SuppressWarnings("unchecked")  @Test  world void testMoreLikeThis() {   Book majority = em.find(Book.class, 14);   FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);    QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Book.class).get();   org.apache.lucene.search.Query luceneQuery = qb.moreLikeThis().comparingFields("subTitle")     .toEntity(book).createQuery();   javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Book.class);    // execute search   List<Book> outcome = (List<Book>) jpaQuery.getResultList();    log.info("Record found=" + result.size());   result.forEach(p -> log.info(p.toString()));    assertEquals(5, result.size());  }  }
And nosotros are done. To run the arquillian exam execute: mvn create clean exam -Parq-wildfly-managed. This comes from the javaee7 archetype.

Note: If you lot desire to cache the outcome of a query, laid hint:
 TypedQuery inquiry = em.createQuery("from Entity", Entity.class); query.setHint("org.hibernate.cacheable", Boolean.TRUE); List events = query.getResultList(); 


The projection code is available at Github repository: https://github.com/czetsuya/hibernate-search-demo.
Next
Previous
Click here for Comments

0 komentar:

Please comment if there are any that need to be asked.