1

对于我的最终项目,我计划在 ejb 模块中实现休眠搜索。由于远程客户端调用索引我的数据库,我有以下异常:

原因:org.hibernate.service.UnknownServiceException:请求未知服务 [org.hibernate.search.hcore.impl.SearchFactoryReference]

请在下面找到我的代码:

1.我的ejb会话:

import javax.annotation.PostConstruct;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.apache.lucene.search.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.query.dsl.QueryBuilder;

/**
 * Session Bean implementation class HibernateIndexer
 */
@Stateless(name="HIND")
@LocalBean
public class HibernateIndexer implements HibernateIndexerRemote, HibernateIndexerLocal {

    protected Configuration config;
    protected SessionFactory sessionFactory;
    protected Session session;
    protected FullTextSession ftx;

    /**
     * Default constructor. 
     */
    public HibernateIndexer() {
        // TODO Auto-generated constructor stub
    }

    @PostConstruct
    private void init(){
        config=new Configuration();
        sessionFactory= config.configure().buildSessionFactory();
        session=sessionFactory.openSession();
        ftx=Search.getFullTextSession(session);     
    }

    public void indexer(){       

    try {
        ftx.createIndexer().startAndWait();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     session.close();
    }

}

2.我的ejb实体

2.1 TgPersonnePer.java

import org.hibernate.search.annotations.Analyze;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.IndexedEmbedded;
import org.hibernate.search.annotations.Store;

@Indexed
public class TgPersonnePer implements java.io.Serializable {

    @DocumentId
    private String perId;
    @IndexedEmbedded
    private TgPersonneMoralePer tgPersonneMoralePer;

    public TgPersonnePer() {
    }

    public TgPersonnePer(String perId) {
        this.perId = perId;
    }
// setter and getter
...................
}

2.2 TgPersonneMoralePer.java

import org.hibernate.search.annotations.Field;

public class TgPersonneMoralePer implements java.io.Serializable {

    private String perId;
    private TgPersonnePer tgPersonnePer;
    @Field
    private String perDenoSociale;
    @Field
    private String perSigle;

    public TgPersonneMoralePer() {
    }

    public TgPersonneMoralePer(TgPersonnePer tgPersonnePer) {
        this.tgPersonnePer = tgPersonnePer;
    }

    // Setter and getter
     .................
}

3. 我的休眠配置文件:hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>      
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
        <property name="hibernate.connection.username">exam</property>
        <property name="hibernate.connection.password">azerty</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.search.autoregister_listeners">true</property>
        <property name="hibernate.validator.apply_to_ddl">false</property>

                <!-- Echo all executed SQL to stdout -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>

        <!--  Hibernate research configuration-->

        <property name="hibernate.search.default.directory_provider">filesystem</property>
        <property name="hibernate.search.default.indexBase">D:\Lucene\indexes</property>


        <property name="hibernate.ejb.event.post-insert">org.hibernate.search.event.FullTextIndexEventListener</property>
        <property name="hibernate.ejb.event.post-update">org.hibernate.search.event.FullTextIndexEventListener</property>
        <property name="hibernate.ejb.event.post-delete">org.hibernate.search.event.FullTextIndexEventListener</property>

        <mapping resource="org/module/entities/TgPersonneMoralePer.hbm.xml" />
        <mapping resource="org/module/entities/TgPersonnePer.hbm.xml" />
    </session-factory>
</hibernate-configuration>

4.我的jboss部署结构:jboss-deployment-structure.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
  <deployment>
    <dependencies>
      <module name="org.hibernate" slot="main"/>
      <module name="org.jboss.logging" slot="main"/>
      <module name="javax.transaction.api" slot="main"/>
      <module name="org.javassist"/>

      <!-- Module de hibernate Search -->
      <module name="org.hibernate.search.orm" slot="main"/>
      <module name="org.hibernate.search.engine" slot="main"/>
      <module name="org.apache.lucene" slot="main"/>
      <module name="org.apache.lucene.internal" slot="main"/>

    </dependencies>
    <exclusions>
      <module name="org.javassist" slot="main"/>
    </exclusions>
  </deployment>
</jboss-deployment-structure>
4

1 回答 1

1

您需要从 Hibernate Search ORM 模块公开服务。

因此,将按顺序执行以下操作:

<module name="org.hibernate.search.orm" services="export" slot="main"/>

添加这个模块应该足以让 Hibernate Search 运行,您不需要添加其他模块(引擎和 lucene)。

于 2017-04-14T14:46:12.190 回答