【发布时间】: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