在JPA中使用MySQL全文查询,而无需使用Hibernate-search,Solr和eclipse-link

时间:2019-05-28 14:18:14

标签: java hibernate jpa full-text-search

当前,我正在使用以下Maven项目的配置。我想在我的代码中使用JPA(JPQL)使用全文搜索查询。

我尝试了这个建议,但找不到与我的以下配置兼容的MySQL5Dialect的maven依赖项。

How to use MySQL's full text search from JPA

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.4.0.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>3.1.0.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.3.1.GA</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>3.4.0.GA</version>

1 个答案:

答案 0 :(得分:0)

我创建了方言自定义类

公共类FullTextSearch扩展了MySQL5InnoDBDialect {

public FullTextSearch(){
    super();
    registerFunction("match", new SQLFunctionTemplate(new DoubleType(),"match(?1) against (?2 in boolean mode)"));
}

}

在代码中,我将此方法称为

 Query namedQuery = em.createQuery("select user from User user " +
                "where match(user.familyName,LOWER(CONCAT(:searchName,'*'))) > 0 " +
                "order by user.familyName");

我在persistence.xml中添加配置

<property name="hibernate.dialect" value="textsearch.FullTextSearch"/>

但是我遇到了错误

WebServiceException: org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException[AbstractEntityManagerImpl.java:614] threw javax.persistence.PersistenceException (org.hibernate.exception.SQLGrammarException: could not execute query)