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.
Domain object:
@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<User> users;
...
}
DAO:
public abstract class GenericPersistenceDao<T> implements IGenericDao<T> {
@PersistenceContext
private EntityManager entityManager;
...
@Override
public FullTextEntityManager getSearchManager() {
return Search.getFullTextEntityManager(entityManager);
}
}
Service:
@Service(value = "roleService")
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleDao roleDAO;
...
@Override
@SuppressWarnings("unchecked")
public List<Role> 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();
}
}
Test:
@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<Role> roles = roleService.searchRoles("test");
assertEquals(1, roles.size()); // returns 0
}
}
Configurations
<persistence-unit name="hibernatePersistence" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" />
<property name="hibernate.search.default.indexBase" value="indexes" />
</properties>
</persistence-unit>
<!-- Entity manager -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="hibernatePersistence" />
</bean>
<!-- Transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Enable the configuration of transaction behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan base-package="org.myproject" />
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?