2015-12-21 101 views
0

我想创建全文索引,并采取具体的搜索结果列表与Hibernate。 我不能超越会话初始化。休眠FullTextSearch异常在SessionInitialize

这里是我的代码:

这是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文件的列表:

enter image description here

回答

0

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

此外,您还有Hibernate ORM 您的类路径上的EclipseLink,它至少为spec包(javax.persistence。*)提供了重复的包。假设你想使用Hibernate Search,你应该远程EclipseLink JAR。

在更一般的说明中,您将从使用Maven,Gradle或Ant + Ivy等依赖管理工具中受益匪浅。

+0

我已经删除了EclipseLink Jars和Hibernate ORM 4.3库。但现在我甚至不能建立项目。现在我正在使用Hibernate 5.0.6。它甚至不建立项目。这就是我得到的:**'org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor'小于-source'1.8'** –

+0

似乎你仍然在某处有一些EclipseLink依赖项。 – Gunnar

+0

没有Eclipse库,它不能正常工作。它现在有效,但我不知道它是如何工作的。我一直在删除和导入jar文件,它工作。现在它可以和Hibernate 5.0.6一起工作,但eclipse库也可以。 –