0
votes

Been stuck on this error for a few days now, any help would be greatly appreciated. Thanks!

Here is the error message from the console:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.HibernateException: Could not parse configuration: /hibernate.cfg.xml at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1491) at org.hibernate.cfg.Configuration.configure(Configuration.java:1425) at org.hibernate.cfg.Configuration.configure(Configuration.java:1411) at hibernateTest.HibernateTest.main(HibernateTest.java:18) Caused by: org.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect at org.dom4j.io.SAXReader.read(SAXReader.java:484) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481) ... 3 more

Here is my 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>

Here is my HibernateTest.java class:

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!");
}
}

Here is my user class:

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;
}
}
2
Can you show your complete code of hibernate.cfg.xml ?anand mishra
paste complete code of hibernate.cfg.xml file. Seems it is a DTD issue.harshavmb
Added the rest of the code. Sorry it didn't show up before for some reason. Had to add 4 spacesDillon

2 Answers

1
votes

it seems like Hibernate is trying to load DTD file to validate the hibernate.cfg.xml file and its failing.To solve this issue you need to add the right DTD in your configuration file.

you need to add the DTD as below in your configuration file and please also check the required jar in your lib.

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

or you may Copy the dtd from hibernate-mapping-3.0.dtd and hibernate-configuration-3.0.dtd which is inside hibernate folder. e,g.

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 

if the above solution didn't solve your problem and you are still getting the same exception then its because, Hibernate is trying to load DTD file to validate the hibernate.cfg.xml file and it was failing because there was no internet connection. So provide the DTD file location in the system using classpath. So the DocType that worked offline would be;

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

or

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

Usually servers are behind the firewall and dont have access to internet. These approach can become handy in those cases. Same configurations will also work for hibernate mapping xml files.

0
votes

I think I figured out your problem. You have the wrong jars. So what happened is you went to a hibernate tutorial website and downloaded a list of jars. They were the wrong ones though. You need to download the jars on the hibernate website and add the ones under the 'required' folder. However you also need a JTA jar because for some reason that jar isn't in the required jar folder.