我正在使用休眠 4 来构建应用程序。在运行应用程序时,我收到以下错误。
无法创建 sessionFactory object.java.lang.NoSuchFieldError: TRACE Exception in thread "main" java.lang.NullPointerException
我的代码片段是,
try{
session = new DBConnection().getSession();
tx= session.beginTransaction();
........
........
}catch(HibernateException ex)
{
if (tx!=null) tx.rollback();
ex.printStackTrace();
session.close();
DBConnection.close();
}
catch(Exception ex)
{
ex.printStackTrace();
session.close();
}
finally{
session.close(); // The error is shown in this line while run time
DBConnection.close();
}
DBConnection.java
public DBConnection()
{
try
{
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
factory = configuration.buildSessionFactory(serviceRegistry);
}
catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public Session getSession()
{
return factory.openSession();
}
// Call this during shutdown
public static void close() {
factory.close();
}
我的配置文件是:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- Assume test is the database name -->
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
test
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">false</property>
<property name="hibernate.connection.pool_size">40</property>
</session-factory>
</hibernate-configuration>
请指导我代码或任何 jar 文件有什么问题?
**
答:log4j 是导致此问题的原因。我删除了 Log4j 并添加了 log4j-1.2.16.jar。它得到修复。谢谢!
**