title: java.lang.NoSuchFieldError: session
body: <p>I am working on a SpringMVC+CRUD+Hibernate Search example :</p>
  
  <p>When I search for a particular text, it gives me this error :</p>
  
  <pre><code>**Error** : HTTP Status 500 - Handler dispatch failed; nested exception is java.lang.NoSuchFieldError: session
  I tried all the examples given here :
  
  http://www.programcreek.com/java-api-examples/index.php?api=org.hibernate.search.jpa.FullTextEntityManager, but nothing works :
  </code></pre>
  
  <p>My Code: </p>
  
  <p>////////////////////////////search.jsp////////////////////////////</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
      &lt;%@ taglib uri="http://www.springframework.org/tags/form"
      prefix="springForm"%&gt;
       &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Search Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;%--   &lt;ol&gt;
      &lt;li th:each="person : ${searchResults}"&gt;
        &lt;b&gt;&lt;span th:text="${person.name}"&gt;&lt;/span&gt;&lt;/b&gt; - 
        &lt;span th:text="${person.address}"&gt;&lt;/span&gt; -
        &lt;span th:text="${person.salary}"&gt;&lt;/span&gt;
        &lt;span th:text="${person.gender}"&gt;&lt;/span&gt;
      &lt;/li&gt;
    &lt;/ol&gt; --%&gt;
    &lt;springForm:form action="/MainAssignment3/search" method="GET"
          commandName="person"&gt;
      &lt;tr&gt;
                  &lt;td&gt;Enter Search Text:&lt;/td&gt;
                  &lt;td&gt;&lt;input type="text" name="text"&gt;
                  &lt;input type="submit" value="submit" onclick="/MainAssignment3/search"&gt;&lt;/input&gt;              &lt;/td&gt;
  
  
  
              &lt;/tr&gt;
          &lt;/springForm:form&gt;
    &lt;table border="1"&gt;
  &lt;tr&gt;
      &lt;th&gt;id&lt;/th&gt;
      &lt;td&gt;name&lt;springForm:errors path='${person.name}' /&gt;&lt;/td&gt;    
          &lt;td&gt;address&lt;springForm:errors path='${person.address}' /&gt;&lt;/td&gt;
      &lt;td&gt;gender&lt;springForm:errors path='${person.gender}' /&gt;&lt;/td&gt;        
  
      &lt;td&gt;salary&lt;springForm:errors path='${person.salary}' /&gt;&lt;/td&gt;&lt;/tr&gt;   
   &lt;c:forEach var="p" items='${searchResults}' varStatus="status"&gt;
      &lt;tr&gt;
  
          &lt;td&gt;${p.id}&lt;/td&gt;
          &lt;td&gt;${p.name}&lt;/td&gt;
          &lt;td&gt;${p.address}&lt;/td&gt;
          &lt;td&gt;${p.gender}&lt;/td&gt;
          &lt;td&gt;${p.salary}&lt;/td&gt;
  
      &lt;/tr&gt;
  
  &lt;/c:forEach&gt; 
  
  
  &lt;/table&gt;
  
   &lt;%--  &lt;p th:if="${searchResults.isEmpty()}"&gt;
      &lt;strong&gt;Hint&lt;/strong&gt;: the query "&lt;a href='/search?query=any'&gt;any&lt;/a&gt;" 
      should return some results.
    &lt;/p&gt; --%&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>/////////////////////////////Implementation of the search method//////////</p>
  
  <pre><code>    package com.dao;
  
  import java.util.List;
  import java.util.Map;
  
  import javax.persistence.Cache;
  import javax.persistence.EntityGraph;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.PersistenceUnitUtil;
  import javax.persistence.SynchronizationType;
  import javax.persistence.criteria.CriteriaBuilder;
  import javax.persistence.metamodel.Metamodel;
  
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.Transaction;
  import org.hibernate.cfg.Configuration;
  import org.hibernate.query.Query;
  import org.hibernate.search.jpa.FullTextEntityManager;
  import org.hibernate.search.jpa.FullTextQuery;
  import org.hibernate.search.jpa.Search;
  import org.hibernate.search.query.dsl.EntityContext;
  import org.hibernate.search.query.dsl.QueryBuilder;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.orm.jpa.EntityManagerHolder;
  import org.springframework.stereotype.Repository;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.model.Person;
  
  @Transactional
  @Repository
  public class PersonDAOImpl implements PersonDAO, java.io.Serializable {
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      //private EntityManager em;
      //private FullTextEntityManager ftem;
      private EntityManagerFactory efact;
  
  
  
  
  
  
      private static final Logger logger = (Logger) LoggerFactory
              .getLogger(PersonDAOImpl.class);
  
      @Autowired
      private SessionFactory sessionFactory;
  
      public void setSessionFactory(SessionFactory sf) {
          this.sessionFactory = sf;
      }
  
      public void save(Person p) {
          // TODO Auto-generated method stub
          Session s = sessionFactory.openSession();
          Transaction tx = s.beginTransaction();
          s.saveOrUpdate(p);
          tx.commit();
          s.close();
  
          System.out.println("Record successfully inserted");
  
      }
  
      @SuppressWarnings("deprecation")
      public List&lt;Person&gt; list() {
          // TODO Auto-generated method stub
          Session session = this.sessionFactory.getCurrentSession();
          @SuppressWarnings("unchecked")
          List&lt;Person&gt; personsList = session.createQuery("from Person").list();
          for (Person p : personsList) {
              logger.info("Person List::" + p);
          }
          return personsList;
  
      }
  
      public void updatePerson(Integer id) {
          Session session = new Configuration().configure().buildSessionFactory()
                  .openSession();
          Person p = new Person();
          Person person = session.get(Person.class, p.getId());
  
  
          session.createQuery("from Person");
          person.setName(p.getName()); // modify the loaded object somehow
          session.update(person);
          //t.commit();
          session.close();
  
      }
  
      public Person getPersonById(int id) {
          // TODO Auto-generated method stub
          Session session = this.sessionFactory.getCurrentSession();
          Person p = (Person) session.load(Person.class, new Integer(id));
          logger.info("Person loaded successfully, Person details=" + p);
          return p;
      }
  
      public void removePerson(Integer id) {
          Session session = sessionFactory.getCurrentSession();
  
  //      Transaction t = session.beginTransaction();
          Person p = (Person) session.load(Person.class, new Integer(id));
          session.delete(p);
  //      t.commit();
          logger.info("Person deleted successfully, person details=");
  
      }
  
      @SuppressWarnings("unchecked")
      public List&lt;Person&gt; search(String text) {
          /*// TODO Auto-generated method stub
      //  FullTextEntityManager ftEntityManager = EntityManagerHolder.getFullTextEntityManager(); 
          //FullTextEntityManager fullTextEntityManager = (FullTextEntityManager) ((EntityContext) entityManager).get();
          FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
          //FullTextSession fullTextEntityManager =Search.getFullTextSession(sessionFactory.getCurrentSession());
          QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Person.class).get();
      org.apache.lucene.search.Query query = queryBuilder.keyword().onFields("name", "address", "salary","gender").matching(text).createQuery();
          //org.hibernate.search.jpa.FullTextQuery jpaQuery = fullTextEntityManager.createFullTextQuery(query, Person.class);
          FullTextQuery jpaQuery= fullTextEntityManager.createFullTextQuery(query, Person.class);
          @SuppressWarnings("unchecked")
          List&lt;Person&gt; results = jpaQuery.getResultList();
          em.refresh(Person.class);
          return results;*/
  /*        // Create a Query Builder
          QueryBuilder qb = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(Person.class).get();
  
          // Create a Lucene Full Text Query
          org.apache.lucene.search.Query luceneQuery = (org.apache.lucene.search.Query) qb.bool()
                  .must(qb.keyword().onFields("name", "address", "salary","gender").matching(text).createQuery());
  
  
          Query fullTextQuery = (Query) getFullTextEntityManager().createFullTextQuery(luceneQuery, Person.class);
  
          // Run Query and print out results to console
          List&lt;Person&gt; result = (List&lt;Person&gt;) fullTextQuery.getResultList();
  
          return result;
  }
  
  
   protected FullTextEntityManager getFullTextEntityManager() {
          if (ftem == null) {
              ftem = Search.getFullTextEntityManager(em);
          }
          return ftem;
      }
  
      *//**
       * Get the JPA Entity Manager (required for the DBUnit Tests).
       * @return Entity manager
       *//*
      protected EntityManager getEntityManager() {
          return em;
      }
  
      *//**
       * Sets the JPA Entity Manager (required to assist with mocking in Unit Test)
       * @param em EntityManager
       *//*
      protected void setEntityManager(EntityManager em) {
          this.em = em;
      }
  
  */          EntityManager em = efact.createEntityManager();
          FullTextEntityManager fullTextEntityManager =
              org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
          em.getTransaction().begin();
  
          QueryBuilder qb = fullTextEntityManager.getSearchFactory()
              .buildQueryBuilder().forEntity(Person.class).get();
          org.apache.lucene.search.Query luceneQuery = qb
            .keyword()
            .onFields("name", "address", "salary","gender")
            .matching(text)
            .createQuery();
  
          // wrap Lucene query in a javax.persistence.Query
          javax.persistence.Query jpaQuery =
              fullTextEntityManager.createFullTextQuery(luceneQuery, Person.class);
  
          // execute search
          List result = jpaQuery.getResultList();
  
          em.getTransaction().commit();
          em.flush();
          em.close();
  
          return result;
  } } 
  </code></pre>
  
  <p>////////////////////////////Controller/////////////////////////////</p>
  
  <pre><code>@Controller
  public class MainController {
  
      @Autowired
      private PersonService ps;
  
      @RequestMapping("/")
      public ModelAndView listPersons(ModelAndView model) throws IOException {
          List&lt;Person&gt; listper = ps.list();
          model.addObject("personsList", listper);
          model.setViewName("index");
          return model;
  
      }
  
      @RequestMapping("/search")
      public ModelAndView search(String query, Model model,HttpServletRequest req)
      {
          String name = req.getParameter("name");
          String address = req.getParameter("address");
          String salary = req.getParameter("salary");
          String gender = req.getParameter("gender");
          model.addAttribute("name", name);
          model.addAttribute("address", address);
          model.addAttribute("salary", salary);
          model.addAttribute("gender", gender);
          List&lt;Person&gt; searchResults = null;
      searchResults = ps.search(query);
         //NullPointerException in the above line
          model.addAttribute("searchResults", searchResults);
          return new ModelAndView("search");
      }
  </code></pre>
  
  <p>/////////////////////////////////ServiceImplementation////////////////////</p>
  
  <pre><code>public List&lt;Person&gt; search(String text) {
      // TODO Auto-generated method stub
      return pdao.search(text);
  }
  </code></pre>
  
  <p>/////////////////////////<strong>stacktrace</strong>//////////////////////</p>
  
  <pre><code>  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springConfig': Unsatisfied dependency expressed through field 'ps': Error creating bean with name 'PersonService': Unsatisfied dependency expressed through field 'pdao': Error creating bean with name 'personDAOImpl' defined in file [C:\spring-tool 3.6. (64 bit)\sts-bundle\pivotal-tc-server-developer-3.0.2.SR2\base-instance\wtpwebapps\MainAssignment3\WEB-INF\classes\com\dao\PersonDAOImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personDAOImpl' defined in file [C:\spring-tool 3.6. (64 bit)\sts-bundle\pivotal-tc-server-developer-3.0.2.SR2\base-instance\wtpwebapps\MainAssignment3\WEB-INF\classes\com\dao\PersonDAOImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'PersonService': Unsatisfied dependency expressed through field 'pdao': Error creating bean with name 'personDAOImpl' defined in file [C:\spring-tool 3.6. (64 bit)\sts-bundle\pivotal-tc-server-developer-3.0.2.SR2\base-instance\wtpwebapps\MainAssignment3\WEB-INF\classes\com\dao\PersonDAOImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personDAOImpl' defined in file [C:\spring-tool 3.6. (64 bit)\sts-bundle\pivotal-tc-server-developer-3.0.2.SR2\base-instance\wtpwebapps\MainAssignment3\WEB-INF\classes\com\dao\PersonDAOImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManager] found for dependency [javax.persistence.EntityManager]: expected at least 1 bean which qualifies as autowire candidate for this dependency.
  </code></pre>
  
  <p>//////////////////////////////////////pom.xml/////////////////</p>
  
  <pre><code>    &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;org.springframework.samples.service.service&lt;/groupId&gt;
      &lt;artifactId&gt;MainAssignment3&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
  
      &lt;properties&gt;
  
          &lt;!-- Generic properties --&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
          &lt;project.reporting.outputEncoding&gt;UTF-8&lt;/project.reporting.outputEncoding&gt;
  
          &lt;!-- Web --&gt;
          &lt;jsp.version&gt;2.2&lt;/jsp.version&gt;
          &lt;jstl.version&gt;1.2&lt;/jstl.version&gt;
          &lt;servlet.version&gt;2.5&lt;/servlet.version&gt;
  
  
          &lt;!-- Spring --&gt;
          &lt;spring-framework.version&gt;4.3.2.RELEASE&lt;/spring-framework.version&gt;
  
          &lt;!-- Hibernate / JPA --&gt;
          &lt;hibernate.version&gt;5.2.1.Final&lt;/hibernate.version&gt;
  
          &lt;!-- Logging --&gt;
          &lt;logback.version&gt;1.0.13&lt;/logback.version&gt;
          &lt;slf4j.version&gt;1.7.5&lt;/slf4j.version&gt;
  
      &lt;/properties&gt;
  
      &lt;dependencies&gt;
      &lt;dependency&gt;
      &lt;groupId&gt;log4j&lt;/groupId&gt;
      &lt;artifactId&gt;log4j&lt;/artifactId&gt;
      &lt;version&gt;1.2.17&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate.javax.persistence&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-jpa-2.0-api&lt;/artifactId&gt;
      &lt;version&gt;1.0.1.Final&lt;/version&gt;
  &lt;/dependency&gt;
  
  
      &lt;dependency&gt;
      &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
      &lt;artifactId&gt;spring-data-jpa&lt;/artifactId&gt;
      &lt;version&gt;1.9.0.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
      &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
      &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
      &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
          &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
      &lt;version&gt;5.1.0.Final&lt;/version&gt;
  &lt;/dependency&gt;
  
          &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-search-engine&lt;/artifactId&gt;
      &lt;version&gt;5.5.4.Final&lt;/version&gt;
  &lt;/dependency&gt;
  
  
  &lt;dependency&gt;
      &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
      &lt;artifactId&gt;hibernate-search-orm&lt;/artifactId&gt;
      &lt;version&gt;5.5.4.Final&lt;/version&gt;
  &lt;/dependency&gt;
  
  
          &lt;!-- Spring MVC --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${spring-framework.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
      &lt;groupId&gt;javax.validation&lt;/groupId&gt;
      &lt;artifactId&gt;validation-api&lt;/artifactId&gt;
      &lt;version&gt;1.1.0.Final&lt;/version&gt;
  &lt;/dependency&gt;
  
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
              &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt; &lt;groupId&gt;org.springframework&lt;/groupId&gt; &lt;artifactId&gt;spring-tx&lt;/artifactId&gt; 
              &lt;version&gt;4.3.2.RELEASE&lt;/version&gt; &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-validator&lt;/artifactId&gt;
              &lt;version&gt;5.2.4.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;mysql&lt;/groupId&gt;
              &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
              &lt;version&gt;6.0.3&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt; &lt;groupId&gt;javax.transaction&lt;/groupId&gt; &lt;artifactId&gt;jta&lt;/artifactId&gt; 
              &lt;version&gt;1.1&lt;/version&gt; &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;5.2.1.Final&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet.jsp-api&lt;/artifactId&gt;
              &lt;version&gt;2.3.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
      &lt;version&gt;${spring-framework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;4.3.2.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Other Web dependencies --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;${jstl.version}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;${servlet.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
              &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
              &lt;version&gt;${jsp.version}&lt;/version&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring and Transactions --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-tx&lt;/artifactId&gt;
              &lt;version&gt;${spring-framework.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Logging with SLF4J &amp; LogBack --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
              &lt;artifactId&gt;slf4j-api&lt;/artifactId&gt;
              &lt;version&gt;${slf4j.version}&lt;/version&gt;
              &lt;scope&gt;compile&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;ch.qos.logback&lt;/groupId&gt;
              &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
              &lt;version&gt;${logback.version}&lt;/version&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Hibernate --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt;
              &lt;version&gt;${hibernate.version}&lt;/version&gt;
          &lt;/dependency&gt;
  
  
          &lt;!-- Test Artifacts --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
              &lt;version&gt;${spring-framework.version}&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;javax.persistence&lt;/groupId&gt;
              &lt;artifactId&gt;persistence-api&lt;/artifactId&gt;
              &lt;version&gt;1.0.2&lt;/version&gt;
          &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
      &lt;version&gt;${spring-framework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  
          &lt;dependency&gt;
      &lt;groupId&gt;junit&lt;/groupId&gt;
      &lt;artifactId&gt;junit&lt;/artifactId&gt;
      &lt;version&gt;4.12&lt;/version&gt;
  &lt;/dependency&gt;
  
  
      &lt;/dependencies&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I tried <strong>two approaches : Hibernate Session and JPA</strong></p>
  
  <p><strong>Approach 1 : JPA</strong></p>
  
  <p>DAO Implementation :</p>
  
  <pre><code> @PersistenceUnit
          private EntityManagerFactory emf;
          private EntityManager em;
          private FullTextEntityManager ftem;
  
          public void createFullTextEntityManager() {
              em = emf.createEntityManager();
              ftem = Search.getFullTextEntityManager(em);
          }
  
  @Autowired
      private SessionFactory sessionFactory;
  
  @SuppressWarnings("unchecked")
      public List&lt;Person&gt; search(String text) throws InterruptedException {
          EntityManager em = emf.createEntityManager();
          FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
  
              em.getTransaction().begin();
  
              fullTextEntityManager.createIndexer().startAndWait();
              QueryBuilder qb = fullTextEntityManager.getSearchFactory()
                      .buildQueryBuilder().forEntity(Person.class).get();
                  org.apache.lucene.search.Query luceneQuery = qb
                    .keyword()
                    .onFields("name", "address", "salary","gender")
                    .matching(text)
                    .createQuery();
  
                  // wrap Lucene query in a javax.persistence.Query
                  javax.persistence.Query jpaQuery =
                      fullTextEntityManager.createFullTextQuery(luceneQuery, Person.class);
  
                  // execute search
                  List result = jpaQuery.getResultList();
  
                  em.getTransaction().commit();
                  em.close();
                  return result;
  
  
  
  
  }}
  </code></pre>
  
  <p><strong>Approach 2 : Hibernate Session</strong></p>
  
  <p>DAOImpl :</p>
  
  <pre><code>@Autowired
      private SessionFactory sessionFactory;
      private Session session=null;
  
      public void setSessionFactory(SessionFactory sf) {
          this.sessionFactory = sf;
      }
  
  
      @SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
      public List&lt;Person&gt; search(String text) throws InterruptedException{
  
          FullTextSession fullTextSession = Search.getFullTextSession(session);
          try {
              Transaction tx = fullTextSession.beginTransaction();
              org.hibernate.search.query.dsl.QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Person.class ).get();
              org.apache.lucene.search.Query query = qb.keyword().onFields("name", "address", "salary","gender").matching(text).createQuery();
              org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(query, Person.class);
               List result = hibQuery.list();
               Iterator&lt;Person&gt; it = result.iterator();
               while (it.hasNext()) {
                     Person p = (Person) it.next();
                     System.out.println(p);
                    }
                    tx.commit();
  
          }
          finally{
              session.close();
          }
  
  
      return null;
      }
  }
  </code></pre>