Hibernate with infinispan tutorial for beginners
Tech-Today

Hibernate with infinispan tutorial for beginners


This tutorial is for developers who are just beginning to learn hibernate-search and trying to set up 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 we need:

Note:
I created my project using javaee7 maven archetype.

Since hibernate-search has already explained a lot we will just take note of the most important pieces:
  1. persistence.xml

    <property name="hibernate.show_sql" value="true" />
    <!-- Enables second level cache, call find after insert and no query in the database will be issue -->
    <property name="hibernate.cache.use_second_level_cache" value="false"/>
    <!-- Enable query 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 here we set the directory_provider to infinispan, then we also set the cache JNDI name. So in wildfly's standalone.xml, we must add 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 class Author and Book from hibernate search documentation.
  3. In our pom we must define the following 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 correct value.
    2. Create our arquillian test class:
      /*
      * JBoss, Home of Professional Open Source
      * Copyright 2013, Red Hat, Inc. and/or its affiliates, and individual
      * contributors by the @authors tag. See the copyright.txt in the
      * distribution for a full listing of individual contributors.
      *
      * Licensed under the Apache License, Version 2.0 (the "License");
      * you may not use this file except in compliance with the License.
      * You may obtain a copy of the License at
      * http://www.apache.org/licenses/LICENSE-2.0
      * Unless required by applicable law or agreed to in writing, software
      * distributed under the License is distributed on an "AS IS" BASIS,
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      * See the License for the specific language governing permissions and
      * limitations under the License.
      */
      package 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)
      public class HibernateSearchTest {

      private Logger log = Logger.getLogger(this.getClass().getName());

      @Inject
      private EntityManager em;

      @Deployment
      public static Archive<?> createTestArchive() {
      return 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 test datasource
      .addAsWebInfResource("test-ds.xml", "test-ds.xml");
      }

      @SuppressWarnings("unchecked")
      @Test
      public void testSimpleJPALuceneSearch() {
      log.info("testSimpleJPALuceneSearch");

      FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);

      // create native Lucene query using the query DSL
      // alternatively you can write the Lucene query using the Lucene query
      // 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();

      // wrap Lucene query in a javax.persistence.Query
      javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Book.class);

      // execute search
      List<Book> result = (List<Book>) jpaQuery.getResultList();

      log.info("Record found=" + result.size());
      result.forEach(p -> log.info(p.toString()));

      assertEquals(9, result.size());
      }

      @SuppressWarnings("unchecked")
      @Test
      public void testMoreLikeThis() {
      Book book = 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> result = (List<Book>) jpaQuery.getResultList();

      log.info("Record found=" + result.size());
      result.forEach(p -> log.info(p.toString()));

      assertEquals(5, result.size());
      }

      }
And we are done. To run the arquillian test execute: mvn clean test -Parq-wildfly-managed. This comes from the javaee7 archetype.

Note: If you want to cache the result of a query, set hint:

TypedQuery query = em.createQuery("from Entity", Entity.class);
query.setHint("org.hibernate.cacheable", Boolean.TRUE);
List events = query.getResultList();


The project code is available at Github repository: https://github.com/czetsuya/hibernate-search-demo.




- Hibernate Ogm For Mongodb
So lately I've been playing with Hibernate OGM MongoDB's latest version 5.4.0.Beta2 but I'm not able to run a demo project created from wildfly-javaee7-war archetype following the documentation. Here are the changes I've made to make...

- Wildfly Server Provisioning Elastic Search Integration
I'm interested and would like to evaluate the integration of Elasticsearch to hibernate-search. I'm using the Wildfly container, however, Wildfly's hibernate-search library is a bit outdated: 5.5.8. so I need to find a way to outdate the jars...

- How To Setup Arquillian Testing With Wildfly
This tutorial requires: Knowledge with GITKnowledge with archetypeRequirements:WildflyeclipseWhat to do:In eclipse create a new maven project: File->New->Other, enter maven in the filter. Select Maven Project. Click next, then next. In the filter...

- Rest Testing With Arquillian In Jboss
This article will explain how we can automate REST web service testing using Arquillian and JBoss web server. First, you must create a javaee6 war (non-blank) project from jboss-javaee6 archetype. This should create a project with Member model, service,...

- Creating Your First Ejb3 Project In Eclipse On Jboss Server
This tutorial will teach you on how to setup and run an ejb application. What you need (noted are where I installed my versions): 1.) Jboss 5 (jboss-5.1.0.GA) 2.) eclipse-jee 3.) ojdbc14 (C:\jboss-5.1.0.GA\server\default\lib) Steps: 1.) Create a new EJB...



Tech-Today








.