title: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead
body: <p>guys. I wanted to create Fulltext search in my Spring Boot application with the help of Hibernate Search. And I faced with such an exception: java.lang.IllegalStateException: Not allowed to create transaction on shared EntityManager - use Spring transactions or EJB CMT instead. It ocuurs on line</p>
  
  <pre><code>entityManager.getTransaction().begin();
  </code></pre>
  
  <p>Here is my code </p>
  
  <pre><code>@Repository
  public class HibernateSearch {
    @PersistenceContext
    private EntityManager entityManager;
  
    @Transactional
    public List&lt;Manual&gt; fulltextSearching(String keyword){
  
      FullTextEntityManager fullTextEntityManager =
              org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
      entityManager.getTransaction().begin();
  
      QueryBuilder qb = fullTextEntityManager.getSearchFactory()
              .buildQueryBuilder().forEntity(Manual.class).get();
      org.apache.lucene.search.Query luceneQuery = qb
              .keyword()
              .onFields("name")
              .matching(keyword)
              .createQuery();
  
      javax.persistence.Query jpaQuery =
              fullTextEntityManager.createFullTextQuery(luceneQuery, Manual.class);
  
      @SuppressWarnings("unchecked")
      List&lt;Manual&gt; result =  jpaQuery.getResultList();
  
      entityManager.getTransaction().commit();
      entityManager.close();
      return result;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My entity:</p>
  
  <pre><code>@Entity
  @Table(name = "manual")
  @Getter
  @Setter
  @NoArgsConstructor
  @Indexed
  public class Manual {
  
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "manual_id")
    private Long id;
  
    @NotNull
    @Column(name = "manual_name")
    @Field
    private String name;
  }
  </code></pre>
  
  <p>My properties:</p>
  
  <pre><code>spring.jpa.properties.hibernate.search.default.directory_provider = filesystem
  spring.jpa.properties.hibernate.search.default.indexBase = D:\\Java
  
  spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  </code></pre>
  
  <p>My Maven dependencies:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-search-orm&lt;/artifactId&gt;
          &lt;version&gt;5.7.1.Final&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;/dependency&gt;
  </code></pre>
  
  <p>If you need additional information to answer my question, say and I will add it. I searched for the net but still can't find the answer.
  I'll be very glad if you resolve my problem.</p>