2015-12-21 103 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庫也可以。 –