java - HibernateSearch ElasticSearch集成错误: Exception in thread "main" java. lang.ExceptionInInitializerError

标签 java hibernate elasticsearch hibernate-search

我正在尝试为 ElasticSearch 集成配置 HibernateSearch。我从 HibernateSearch 收到以下错误:

我使用 Oracle 数据库并在我的 pom.xml 文件中添加了所需的依赖项

请找出我遇到的错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
at com.test.webservice.elasticsearch.App.displayContactTableData(App.java:50)
at com.test.webservice.elasticsearch.App.main(App.java:70)
Caused by: java.lang.NullPointerException
at org.hibernate.boot.internal.MetadataBuilderImpl$MetadataBuildingOptionsImpl.<init>(MetadataBuilderImpl.java:579)
at org.hibernate.boot.internal.MetadataBuilderImpl.<init>(MetadataBuilderImpl.java:127)
at org.hibernate.boot.MetadataSources.getMetadataBuilder(MetadataSources.java:135)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:654)
at com.test.webservice.elasticsearch.HibernateUtil.configureSessionFactory(HibernateUtil.java:36)
at com.test.webservice.elasticsearch.HibernateUtil.<clinit>(HibernateUtil.java:44)
... 2 more

它在 HibernateUtil.java 中针对以下行抛出错误:

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);  

正在使用所有最新的依赖项。

hibernate-search-orm ->5.9.1.Final  
hibernate-core ->5.2.16.Final 
ojdbc14 -> 10.2.0.4.0

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>

    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
    <property name="hibernate.connection.username">testdb</property>
    <property name="hibernate.connection.password">123456</property>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="show_sql">false</property>
    <property name="format_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.search.default.directory_provider">filesystem</property>
    <property name="hibernate.search.default.indexBase">C:\lucene\product\indexes</property>

    <mapping class="com.test.webservice.model.Product" />

    </session-factory>
</hibernate-configuration>

HibernateUtil.java

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

    public class HibernateUtil {

        private static SessionFactory sessionFactory = null;  
        private static ServiceRegistry serviceRegistry = null;  

        private static SessionFactory configureSessionFactory() throws HibernateException {  
        Configuration configuration = new Configuration();  
        configuration.configure();  

        Properties properties = configuration.getProperties();
        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(properties).build();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);  

        return sessionFactory;  
    }
    static {
        configureSessionFactory();
    }

    private HibernateUtil() {}

    public static Session getSession() {
        return sessionFactory.openSession();
    }
}

应用程序.java

public class App 
{
    private static void doIndex() throws InterruptedException {
        Session session = HibernateUtil.getSession();

        FullTextSession fullTextSession = Search.getFullTextSession(session);
        fullTextSession.createIndexer().startAndWait();

        fullTextSession.close();
    }

    private static List<Product> search(String queryString) {
        Session session = HibernateUtil.getSession();
        FullTextSession fullTextSession = Search.getFullTextSession(session);

        QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Product.class).get();
        org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("name").matching(queryString).createQuery();

        // wrap Lucene query in a javax.persistence.Query
        org.hibernate.Query fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Product.class);

        List<Product> productList = fullTextQuery.list();

        fullTextSession.close();

        return productList;
    }

    private static void displayContactTableData() {
        Session session = null;

        try {
            session = HibernateUtil.getSession();

            // Fetching saved data
            List<Product> productList = session.createQuery("from Product").list();

            for (Product product : productList) {
                System.out.println(product);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally{
            if(session != null) {
                session.close();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("\n\n******Data stored in Contact table******\n");
        displayContactTableData();

        // Create an initial Lucene index for the data already present in the database
        doIndex();

        Scanner scanner = new Scanner(System.in);
        String consoleInput = null;

        while (true) {
            // Prompt the user to enter query string
            System.out.println("\n\nEnter search key (To exit type 'X')");      
            System.out.println();
            consoleInput = scanner.nextLine();

            if("X".equalsIgnoreCase(consoleInput)) {
                System.out.println("End");
                System.exit(0);
            }   

            List<Product> result = search(consoleInput);            
            System.out.println("\n\n>>>>>>Record found for '" + consoleInput + "'");

            for (Product product : result) {
                System.out.println(product);
            }               
        }           
    }
}

最佳答案

您的代码声明了两个 ServiceRegistry变量:srserviceRegistryserviceRegistry包含null当你将它传递给configuration.buildSessionFactory时,因为您将值放入 sr .

选择一个变量,正确使用它,删除另一个,就应该没问题了。

关于java - HibernateSearch ElasticSearch集成错误: Exception in thread "main" java. lang.ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50002077/

相关文章:

java - "Repeated column in mapping for collection"用于具有相同外键的 hibernate 多对多?

apache - apache错误的logstash grok模式是什么?

indexing - 重新启动Elasticsearch时为什么会显示已删除的索引?

java - TLS v1.2 上的 Android 客户端/服务器

java - 递归分区排序效率低下

hibernate - UnsupportedOperationException 与 hibernate 和 JPA 合并保存多对多关系

java - Hibernate、JPA - 具有两个表的多对多示例

java - Hibernate 多态 HQL SELECT 语句

elasticsearch - 包含带有cURL的新行的批量索引文本字段

java - 设置System.setOut();到命令提示符