我正在尝试将休眠搜索集成到我的项目中。我的模型已编入索引,但由于某种原因,我的搜索查询没有返回任何结果。我已经尝试解决这个问题几个小时了,但我所做的似乎没有任何效果。
域对象:
@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;
...
}
道:
public abstract class GenericPersistenceDao<T> implements IGenericDao<T> {
@PersistenceContext
private EntityManager entityManager;
...
@Override
public FullTextEntityManager getSearchManager() {
return Search.getFullTextEntityManager(entityManager);
}
}
服务:
@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();
}
}
测试:
@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
}
}
配置
<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" />
数据库实际上填充了与该权限字段值匹配的角色。实体管理器是有效的,因为我所有的常规 CRUD 测试都成功了。这意味着该错误完全与休眠搜索(3.1.1.GA)相关,但它哪里出错了?