gpt4 book ai didi

java - 无法创建 sessionFactory 对象.org.hibernate.HibernateException : could not init listeners

转载 作者:太空宇宙 更新时间:2023-11-04 13:02:47 24 4
gpt4 key购买 nike

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

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

(用引号谷歌搜索,什么也找不到)

我是 Hibernate 新手,我想将其添加到现有的 web 应用程序项目中,但有以下限制:

  • Java 1.5.0_22

  • eclipse 赫利俄斯

  • Oracle 9

所以我选择兼容的Hibernate 3.6.10。

一切都可以编译。 jar 和构建路径似乎没问题。在 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>

此类包含文档详细信息。

  </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"/>

我的测试类(class):

    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();
}
}
}

预先感谢您的帮助!朱塞佩

最佳答案

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

您需要使用

Hibernate 3.6.0.Final 为 Hibernate Search 3.3.0.Final

Hibernate 3.6.3.Final 为 Hibernate Search 3.4.0.Final

关于java - 无法创建 sessionFactory 对象.org.hibernate.HibernateException : could not init listeners,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34791880/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com