簡體   English   中英

如何在JBoss datagrid中使用hibernate-search?

[英]How to use hibernate-search with JBoss datagrid?

我有使用JBoss Datagrid停止某些文檔並使之可搜索的要求。 因此,我正在考慮使用已經屬於JBoss的Hibernate-search。 但是現在我被卡住了。 這是我在JUnit中生成的簡單代碼。

@Test
public void testName() throws Exception {
    Cache<Object, Object> c = new DefaultCacheManager().getCache();
    c.put("id", new ProductDG("ean", "desc", newArrayList("image1")));

    ProductDG product = (ProductDG) c.get("id");
    assertTrue(product.getEAN().equals("ean"));

    SessionFactory factory = new AnnotationConfiguration().buildSessionFactory();
    Session session = factory.openSession();

    FullTextSession fts = getFullTextSession(session);
    fts.index(product);

}


@Indexed(index="Product")
class ProductDG {
    private String EAN;
    private String description;

    private List<String> images;

    ProductDG(String EAN, String description, List<String> images) {
        this.EAN = EAN;
        this.description = description;
        this.images = images;
    }

    String getEAN() {
        return EAN;
    }

    String getDescription() {
        return description;
    }

    List<String> getImages() {
        return images;
    }
}

但是我有這樣的錯誤。

org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:97)
    at org.hibernate.service.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:67)
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:174)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1797)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1755)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840)
    at com.tesco.adapters.JBossDataGrid.testName(JBossDataGrid.java:27)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我以為我沒有要連接的數據庫。 這將如何工作? 有人告訴我必須使用JBoss Datagrid。 我知道https://docs.jboss.org/hibernate/ogm/3.0/reference/en-US/html_single/,但它太新了,幾個月前才問世。 可以做到嗎?

您是正確的,JBoss Data Grid使用了Hibernate Search,但是它僅用於索引和查詢定義:您不需要通過“傳統” Hibernate Session使用它。

  • 您不需要啟動Hibernate SessionFactory
  • 您甚至不需要在類路徑中使用Hibernate ORM jar
  • 您確實使用了Hibernate Search中的注釋

要創建一個受您的域啟發的示例,要使用它,只需在Cache配置中啟用索引。

@Test
public void testName() throws Exception {
   Cache<Object, Object> c = new DefaultCacheManager("configuration-file.xml").getCache();
   c.put("id", new ProductDG("ean", "desc", newArrayList("image1")));
   //at this point your Product is already indexed (assuming you don't use transactions)

   //Using the MatchAllDocsQuery we'll get a list of all instances:
   CacheQuery query = searchManager.getQuery(new MatchAllDocsQuery(), ProductDG.class);

   // Execute Query
   List objectList = query.list();

   for (Object prod : objectList)
      System.out.println(prod);

}

您的最后一個問題是關於Hibernate OGM。 是的,也可以這樣做,但是您可能想要閱讀版本4的文檔:

https://docs.jboss.org/hibernate/ogm/4.0/reference/en-US/html_single/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM