【问题标题】:Hibernate FullTextSearch Exception at SessionInitializeSessionInitialize 的休眠全文搜索异常
【发布时间】:2015-12-22 07:25:01
【问题描述】:

我正在尝试使用 Hibernate 创建全文索引并将特定搜索结果作为列表。 我不能超越会话初始化。

这是我的代码:

这是 HibernateUtil 类:

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {
    try {
        // Create the SessionFactory from standard (hibernate.cfg.xml) 
        // config file.
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception. 
        System.err.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}
}

这是我启动 FullTextSession 的代码:

try {
        fullTextSession = Search.getFullTextSession(HibernateUtil.getSessionFactory().getCurrentSession());
        fullTextSession.createIndexer().startAndWait();
    } catch (InterruptedException ex) {
        Logger.getLogger(SatisYonetimiEkrani.class.getName()).log(Level.SEVERE, null, ex);
    }

这是我要进行全文搜索的功能:

private void aramaYap() {
    QueryBuilder productQb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Satislar.class).get();
    Query fullTextQuery = (Query) productQb.keyword().onField("musteriadi").matching("Ramazan").createQuery();

    List<Satislar> satis = fullTextQuery.list();

    for (int i = 0; i < satis.size(); i++) {
        System.out.println(satis.get(i).getMusteriadi());
    }
}

我认为我的 jar 文件可能有问题。以下是 jar 文件列表:

【问题讨论】:

    标签: java hibernate lucene full-text-search hibernate-search


    【解决方案1】:

    您的 Hibernate ORM (4.3) 和 Hibernate Search (5.5) 版本不匹配。 Hibernate Search 5.5 旨在与 ORM 5.x 一起使用(5.0.6 是目前最新的)。

    您的类路径上还有 Hibernate ORM EclipseLink,至少对于规范包 (javax.persistence.*),它会为您提供重复的包。假设您想使用 Hibernate Search,您应该远程 EclipseLink JAR。

    一般来说,使用 Maven、Gradle 或 Ant+Ivy 等依赖项管理工具将使您受益匪浅。

    【讨论】:

    • 我删除了 EclipseLink Jars 和 Hibernate ORM 4.3 库。但现在我什至不能建立项目。现在我正在使用 Hibernate 5.0.6。它甚至不构建项目。这就是我得到的:'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor'小于-source'1.8'
    • 似乎您在某处仍有一些 EclipseLink 依赖项。
    • 如果没有 Eclipse 库,它就无法工作。它现在可以工作,但我也不知道它是如何工作的。我一直在删除和导入 jar 文件,它工作正常。现在它适用于 Hibernate 5.0.6,但也有 eclipse 库。
    • 您必须删除 EclipseLink 库,并重写您的 HibernateUtil。这不是在 Hibernate 5 中启动 SessionFactory 的方式。
    猜你喜欢
    • 1970-01-01
    • 2017-03-19
    • 2011-02-14
    • 2011-11-17
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多