无法创建sessionFactory object.org.hibernate.HibernateException:无法初始化侦听器

时间:2016-01-14 14:20:47

标签: java eclipse oracle hibernate

使用Eclipse + Hibernate + Oracle 9,我的测试应用程序(在Eclipse内部运行)返回给我这个错误:

"Failed to create sessionFactory object.org.hibernate.HibernateException: could not init listeners"

(用引号搜索,无法找到)

我是Hibernate的新手,我希望将它添加到现有的webapp项目中,并具有以下限制:

  • Java 1.5.0_22

  • Eclipse Helios

  • Oracle 9

所以我选择兼容的Hibernate 3.6.10。

一切都在编译。罐子和构建路径似乎没问题。 在Eclipse中运行我的应用程序,控制台显示以下内容:

- Hibernate Commons Annotations 3.2.0.Final
- Hibernate 3.6.10.Final
- hibernate.properties not found
- Bytecode provider name : javassist
- using JDK 1.4 java.sql.Timestamp handling
- configuring from resource: ext\als\hibernate\hibernate.cfg.xml
- Configuration resource: ext\als\hibernate\hibernate.cfg.xml
- Reading mappings from resource : ext\als\hibernate\mappings\Document.hbm.xml
- Configured SessionFactory: null
- Mapping class: ext.als.xcg.mrp.traceability.beans.Document -> DOCUMENT
- Using Hibernate built-in connection pool (not for production use!)
- Hibernate connection pool size: 1
- autocommit mode: false
- using driver: oracle.jdbc.OracleDriver at URL: jdbc:oracle:thin:@localhost:1521:wind
- connection properties: {user=mrp, password=****}
- Using dialect: org.hibernate.dialect.Oracle9iDialect
- Could not obtain connection metadata
- Using default transaction strategy (direct JDBC transactions)
- No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
- Automatic flush during beforeCompletion(): disabled
- Automatic session close at end of transaction: disabled
- JDBC batch size: 15
- JDBC batch updates for versioned data: disabled
- Scrollable result sets: enabled
- JDBC3 getGeneratedKeys(): disabled
- Connection release mode: auto
- Default batch fetch size: 1
- Generate SQL with comments: disabled
- Order SQL updates by primary key: disabled
- Order SQL inserts for batching: disabled
- Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
- Using ASTQueryTranslatorFactory
- Query language substitutions: {}
- JPA-QL strict compliance: disabled
- Second-level cache: enabled
- Query cache: disabled
- Cache region factory : org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge
- Cache provider: org.hibernate.cache.NoCacheProvider
- Optimize cache for minimal puts: disabled
- Structured second-level cache entries: disabled
- Echoing all SQL to stdout
- Statistics: disabled
- Deleted entity synthetic identifier rollback: disabled
- Default entity-mode: pojo
- Named query checking : enabled
- Check Nullability in Core (should be disabled when Bean Validation is on): enabled
Failed to create sessionFactory object.org.hibernate.HibernateException: could not init listeners
Exception in thread "main" java.lang.ExceptionInInitializerError
    at ext.als.xcg.mrp.traceability.manager.DocumentManager.main(DocumentManager.java:26)
Caused by: org.hibernate.HibernateException: could not init listeners
    at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:205)
    at org.hibernate.cfg.Configuration.getInitializedEventListeners(Configuration.java:2010)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at ext.als.xcg.mrp.traceability.manager.DocumentManager.main(DocumentManager.java:21)
Caused by: java.lang.ClassCastException: org.hibernate.cfg.Configuration
    at org.hibernate.search.event.FullTextIndexEventListener.initialize(FullTextIndexEventListener.java:82)
    at org.hibernate.event.EventListeners$1.processListener(EventListeners.java:198)
    at org.hibernate.event.EventListeners.processListeners(EventListeners.java:181)
    at org.hibernate.event.EventListeners.initializeListeners(EventListeners.java:194)
    ... 3 more

这是我的源代码和配置文件:

hibernate.cfg.xml中:

<?xml version="1.0"?>

<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:wind</property>
    <property name="connection.username">mrp</property>
    <property name="connection.password">mrp</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">update</property>

    <!-- to solve java.lang.NoSuchMethodException: org.hibernate.validator.ClassValidator  error -->
    <property name="hibernate.validator.apply_to_ddl">false</property>
    <property name="hibernate.validator.autoregister_listeners">false</property> 
    <property name="hibernate.jdbc.use_get_generated_keys">false</property>



    <!-- List of XML mapping files -->
    <mapping resource="ext\als\hibernate\mappings\Document.hbm.xml"/>




</session-factory>

我的对象映射

<hibernate-mapping>

                该类包含Document详细信息。              

  </id>
  <property name="docNumber" column="DOC_NUMBER" type="string"/>
  <property name="docVersion" column="DOC_VERSION" type="string"/>
  <property name="xcgFile" column="XCG_FILE" type="string"/>
  <property name="xcgDate" column="XCG_DATE" type="date"/>

我的测试课程:

    package ext.als.xcg.mrp.traceability.manager;

import java.util.List; 
import java.util.Date;
import java.util.Iterator; 

import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import ext.als.xcg.mrp.traceability.beans.Document;

public class DocumentManager {

    private static SessionFactory factory; 

    public static void main(String[] args) {
        try{
            factory = new Configuration().configure("ext\\als\\hibernate\\hibernate.cfg.xml").buildSessionFactory();
            //factory = new Configuration().configure().buildSessionFactory();

        }catch (Throwable ex) { 
            System.err.println("Failed to create sessionFactory object." + ex);
            throw new ExceptionInInitializerError(ex); 
        }

        DocumentManager DM = new DocumentManager();

        /* Add few employee records in database */
        Integer docID1 = DM.addDocument("DOC001", "00", "file_1.csv", new Date());
        Integer docID2 = DM.addDocument("DOC002", "02", "file_1.csv", new Date());
        Integer docID3 = DM.addDocument("DOC003", "05", "file_1.csv", new Date());

        /* List down all the employees */
        DM.listDocuments();

        /* Delete an employee from the database */
        DM.deleteDocument(docID2);

        /* List down new list of the employees */
        DM.listDocuments();
    }
    /* Method to CREATE an employee in the database */
    public Integer addDocument(String docNumber, String docVersion, String xcgFile, Date xcgDate){
        Session session = factory.openSession();
        Transaction tx = null;
        Integer documentID = null;
        try{
            tx = session.beginTransaction();
            Document document = new Document(docNumber, docVersion, xcgFile, xcgDate);
            documentID = (Integer) session.save(document); 
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace(); 
        }finally {
            session.close(); 
        }
        return documentID;
    }



    /* Method to  READ all the documents */
    public void listDocuments( ){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            List documents = session.createQuery("FROM Document").list(); 
            for (Iterator iterator = 
                documents.iterator(); iterator.hasNext();){
                Document document = (Document) iterator.next(); 
                System.out.println("Document Number: " + document.getDocNumber()); 
                System.out.println("Document Version:" + document.getDocVersion()); 
                System.out.println("Xcg File:" + document.getXcgFile()); 
                System.out.println("Xcg Date:" + document.getXcgDate());
            }
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace(); 
        }finally {
            session.close(); 
        }
    }


    /* Method to DELETE an employee from the records */
    public void deleteDocument(Integer documentID){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Document document = 
                (Document)session.get(Document.class, documentID); 
            session.delete(document); 
            tx.commit();
        }catch (HibernateException e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace(); 
        }finally {
            session.close(); 
        }
    }
}

提前感谢您的帮助! 朱塞佩

1 个答案:

答案 0 :(得分:0)

看起来你有不兼容的Hibernate和Hibernate Search jar版本。

您需要使用

Hibernate 3.6.0.Final Hibernate Search 3.3.0.Final

Hibernate 3.6.3.Final with Hibernate Search 3.4.0.Final