title: hibernate search + spring3 + jpa
body: <p>I'm trying to integrate hibernate search in my project. My models are indexed, but for some reason my search queries do not return any results. I've been trying to solve this issue for a few hours now, but nothing I do seems to work.</p>
  
  <p>Domain object:</p>
  
  <pre><code>@Entity
  @Table(name = "roles")
  @Indexed
  public class Role implements GrantedAuthority {
  private static final long serialVersionUID = 8227887773948216849L;
  
      @Id @GeneratedValue
      @DocumentId
      private Long ID;
  
      @Column(name = "authority", nullable = false)
      @Field(index = Index.TOKENIZED, store = Store.YES)
      private String authority;
  
      @ManyToMany
      @JoinTable(name = "user_roles", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "username") })
      @ContainedIn
      private List&lt;User&gt; users;
  
      ...
  
  }
  </code></pre>
  
  <p>DAO:</p>
  
  <pre><code>public abstract class GenericPersistenceDao&lt;T&gt; implements IGenericDao&lt;T&gt; {
  
  @PersistenceContext
  private EntityManager entityManager;
  
  ...
  
      @Override
      public FullTextEntityManager getSearchManager() {
          return Search.getFullTextEntityManager(entityManager);
      }
  
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>@Service(value = "roleService")
  public class RoleServiceImpl implements RoleService {
  
      @Autowired
      private RoleDao roleDAO;
  
      ...
  
      @Override
      @SuppressWarnings("unchecked")
      public List&lt;Role&gt; searchRoles(String keyword) throws ParseException {
          FullTextEntityManager manager = roleDAO.getSearchManager();
          TermQuery tquery = new TermQuery(new Term("authority", keyword));
          FullTextQuery query = manager.createFullTextQuery(tquery, Role.class);
          return query.getResultList();
      }
  
  }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:applicationContext.xml" })
  @Transactional
  public class TestRoleService extends Assert {
  
      @Autowired
      private RoleService roleService;
  
      @Test
      public void testSearchRoles() {
         roleService.saveRole(/* role with authority="test" */);
         List&lt;Role&gt; roles = roleService.searchRoles("test");
         assertEquals(1, roles.size()); // returns 0
      }
  
  }
  </code></pre>
  
  <p>Configurations</p>
  
  <pre><code>&lt;persistence-unit name="hibernatePersistence" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" /&gt;
              &lt;property name="hibernate.search.default.indexBase" value="indexes" /&gt;
          &lt;/properties&gt;
  &lt;/persistence-unit&gt;
  
  &lt;!-- Entity manager --&gt;
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="hibernatePersistence" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Transaction manager --&gt;
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Enable the configuration of transaction behavior based on annotations --&gt;
  &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;context:component-scan base-package="org.myproject" /&gt;
  </code></pre>
  
  <p>The database is in fact filled with a role matching that authority field value. The entity manager is valid as all my regular CRUD tests succeed. Meaning the error is entirely hibernate search(3.1.1.GA) related, but where does it go wrong?</p>