0

几天以来一直卡在这个错误上,任何帮助将不胜感激。谢谢!

这是来自控制台的错误消息:

log4j:WARN 找不到记录器(org.hibernate.cfg.Environment)的附加程序。log4j:WARN 请正确初始化 log4j 系统。线程“主”org.hibernate.HibernateException 中的异常:无法解析配置: org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491) 中 org.hibernate.cfg.Configuration.configure 中的/hibernate.cfg.xml (Configuration.java:1425) at org.hibernate.cfg.Configuration.configure(Configuration.java:1411) at hibernateTest.HibernateTest.main(HibernateTest.java:18) 原因:org.dom4j.DocumentException:连接超时:连接嵌套异常:连接超时:连接 在 org.dom4j.io.SAXReader.read(SAXReader.java:484) 在 org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) ... 还有 3 个

这是我的 hibernate.cfg.xml:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>

    <!-- Database connection settings -->
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="connection.url">jdbc:oracle:thin:@ora-gpldev.xyz.com:1242:DUO231D</property>
    <property name="connection.username">user</property>
    <property name="connection.password">pass</property>

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

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

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</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">create</property>

    <!-- Names the annotated entity class -->
    <mapping class="helloHibernate.User"/>

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

这是我的 HibernateTest.java 类:

package hibernateTest;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import helloHibernate.User;

public class HibernateTest {

public static void main(String[] args) {
    User user = new User();
    user.setUserId(1);
    user.setUserName("TheOne");

    SessionFactory sessionFactory = new    Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();
    session.save(user);
    session.getTransaction().commit(); 
    System.out.println("Saved!");
}
}

这是我的用户类:

package helloHibernate;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name= "USER.USER2_TABLE")
public class User {

@Id
private int userId;
private String userName;

public void setUserId(int userId){
    this.userId = userId; 
}

public int getUserId() {
    return userId;
}

public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
}
4

2 回答 2

1

似乎 Hibernate 正在尝试加载 DTD 文件以验证 hibernate.cfg.xml 文件及其失败。要解决此问题,您需要在配置文件中添加正确的 DTD。

你需要在你的配置文件中添加如下的 DTD 并且请检查你的库中所需的 jar。

<?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 文件夹中的 hibernate-mapping-3.0.dtd 和 hibernate-configuration-3.0.dtd 复制 dtd。例如。

hibernate-core-4.3.5.Final.jar\org\hibernate\hibernate-configuration-3.0.dtd 
hibernate-core-4.3.5.Final.jar\org\hibernate\hibernate-mapping-3.0.dtd 

如果上述解决方案没有解决您的问题并且您仍然遇到相同的异常,那么它是因为 Hibernate 正在尝试加载 DTD 文件以验证 hibernate.cfg.xml 文件并且由于没有互联网连接而失败。因此,使用类路径提供系统中的 DTD 文件位置。所以离线工作的 DocType 将是;

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">

或者

<!DOCTYPE hibernate-configuration SYSTEM 
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

通常服务器位于防火墙后面,无法访问互联网。在这些情况下,这些方法会变得很方便。相同的配置也适用于休眠映射 xml 文件。

于 2015-12-30T06:53:41.087 回答
0

我想我发现了你的问题。你有错误的罐子。所以发生的事情是你去了一个hibernate教程网站并下载了一个jar列表。但他们是错误的。您需要在 hibernate 网站上下载 jar 并将它们添加到“必需”文件夹下。但是,您还需要一个 JTA jar,因为由于某种原因该 jar 不在所需的 jar 文件夹中。

于 2015-12-31T16:28:18.467 回答