【问题标题】:Hibernate can't parse cfg file/create session factoryHibernate 无法解析 cfg 文件/创建会话工厂
【发布时间】:2016-04-03 14:44:16
【问题描述】:

这个错误已经卡了几天了,任何帮助将不胜感激。谢谢!

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

log4j:WARN 找不到记录器的附加程序(org.hibernate.cfg.Environment)。 log4j:WARN 请正确初始化 log4j 系统。 线程“主”org.hibernate.HibernateException 中的异常:无法解析配置:/hibernate.cfg.xml 在 org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491) 在 org.hibernate.cfg.Configuration.configure(Configuration.java:1425) 在 org.hibernate.cfg.Configuration.configure(Configuration.java:1411) 在 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;
}
}

【问题讨论】:

  • 你能展示你完整的hibernate.cfg.xml代码吗?
  • 粘贴hibernate.cfg.xml文件的完整代码。似乎这是一个 DTD 问题。
  • 添加了其余代码。很抱歉之前因为某种原因没有出现。必须添加 4 个空格

标签: hibernate sessionfactory hibernate.cfg.xml


【解决方案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 文件,但由于没有 Internet 连接而失败。 因此,使用类路径提供系统中的 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 文件。

【讨论】:

  • 请删除配置文件中标签 之后的空格。如果您不使用远程数据库,请使用主机名作为 localhost。
  • 我实际上正在使用远程数据库。我已经使用相同的 URL 连接使用 jdbc 来测试它并且工作正常。只有休眠会给我带来问题>
  • 请发布您的完整 cfg 文件和错误跟踪日志
  • 这是完整的错误跟踪日志和cfg文件>
  • 您的互联网连接良好。将您的 dtd 替换为 ttp://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" rel="nofollow" target="_blank">hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
【解决方案2】:

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

【讨论】:

  • 您的权利有效。很奇怪,当您下载休眠时,所有必需的 jar 都不在所需的 jar 文件夹下。哇没有意义。感谢您的帮助!很高兴它现在可以工作了:D
猜你喜欢
  • 2016-04-07
  • 2011-12-20
  • 2016-09-15
  • 2014-08-26
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
相关资源
最近更新 更多