J'essaie d'intégrer la recherche hibernate dans mon projet. Mes modèles sont indexés, mais pour une raison quelconque, mes requêtes de recherche ne renvoient aucun résultat. J'ai essayé de résoudre ce problème pendant quelques heures maintenant, mais rien de ce que je fais ne semble fonctionner.hibernate search + spring3 + jpa
objet Domaine:
@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;
...
}
OAC:
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" />
La base de données est en fait rempli d'un rôle correspondant à la valeur du champ d'autorité. Le gestionnaire d'entités est valide car tous mes tests CRUD réguliers réussissent. Ce qui signifie que l'erreur est entièrement liée à la recherche hibernate (3.1.1.GA), mais où cela se passe-t-il?
Etes-vous sûr que votre base de données prend en charge la recherche en texte intégral? – Roman
J'utilise HSQLDB. – Jeroen
correction: les objets de domaine ne sont pas correctement indexés. – Jeroen