编程语言
首页 > 编程语言> > java – hibernate search spring3 jpa

java – hibernate search spring3 jpa

作者:互联网

我正在尝试将hibernate搜索集成到我的项目中.我的模型已编制索引,但由于某些原因,我的搜索查询不会返回任何结果.我一直试图解决这个问题几个小时,但我做的事似乎没有用.

域对象:

@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(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测试都成功.意思是错误完全与hibernate搜索(3.1.1.GA)有关,但哪里出错?

解决方法:

理论上它一切正常但可能存在一些问题:

>您最初索引现有对象吗?虽然Hibernate Search索引所有新的更改,但它不知道预先存在的对象,因此您需要最初索引它们(使用ftem #index())
>默认情况下,HSearch挂钩到Hibernate或JTA事务以在事务事件之前和之后进行侦听.也许你的Spring tx配置会绕过它,因此HSearch不会被触发,因此无法索引.最好的方法是使用真正的JTA事务管理器并避免这些外观.
>如果您正在谈论初始索引(使用index()),您也可以使用#flushToIndexes()来强制索引,即使未提交tx也是如此.
>最后但并非最不重要的是,您的最后一段代码可能会抛出OutOfMemoryException,因为您在索引它们之前加载内存中的所有对象.查看Hibernate Search参考文档,了解如何正确地批量索引对象的加载. Manning的Hibernate Search in Action(我是作者)也深入研究了这一切.

标签:java,spring,jpa,hibernate-search
来源: https://codeday.me/bug/20190627/1301141.html