1

Please help me to find why am getting exception with Hibernate but JDBC works.

PATJ : WorkPlace\Hibernate\src\hibernate.cfg.xml

JDBC :

Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:devt2x", "scott","tiger");
if (connection != null) {
    System.out.println("You made it, take control your database connection.getCatalog());
            }

Output:

You made it, take control your database now!

Bean :

package leo.beans;

import java.util.Date;

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

@Entity
@Table(name = "LEO_USER")
public class User {
@Id @GeneratedValue
private int userId;
private String userName;
private Date dob;
private String address;

public int getUserId() {
    return userId;
}
public void setUserId(int userId) {
    this.userId = userId;
}
public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public Date getDob() {
    return dob;
}
public void setDob(Date dob) {
    this.dob = dob;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
}

Main :

package leo.test;
import java.util.Date;
import leo.beans.User;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;

public class UserSaveTest1 {
    public static void main(String args[]) {
        System.out.println("Hi How are you.........");
        SessionFactory sf = new AnnotationConfiguration().configure().addClass(User.class).buildSessionFactory();

        User user1 = new User();
        user1.setUserId(1);
        user1.setUserName("leo1");
        user1.setDob(new Date());
        user1.setAddress("address1");

        Session session = sf.openSession();
        Transaction trans = session.beginTransaction();
        session.save(user1);
        trans.commit();
        session.close();        
    }
}

Hibernate :

<hibernate-configuration>
    <session-factory>       
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:devt2x</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="hibernate.connection.password">tiger</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>      
        <property name="show_sql">true</property>           
    </session-factory>

</hibernate-configuration>

Exception :

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
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 leo.test.UserSaveTest1.main(UserSaveTest1.java:17)
Caused by: org.dom4j.DocumentException: Connection refused: connect Nested exception: Connection refused: connect
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1481)
    ... 3 more
sunleo
  • 10,589
  • 35
  • 116
  • 196
  • sorry what you mean by source of connection? – sunleo Nov 16 '13 at 05:34
  • updated the path of xml and main code. – sunleo Nov 16 '13 at 06:02
  • `"url"` is not a valid URL. And it is impossible for the connection to be non-null at the point you are testing it. – user207421 Nov 18 '13 at 09:24
  • @sunleo I generally do URL declaration as `jdbc:oracle:thin://localhost:1521/devt2x`. Try changing like this. – RAS Nov 19 '13 at 14:25
  • verify You internet options like here [eclipse-prefrences-network-connections](http://stackoverflow.com/questions/5456480/eclipse-prefrences-network-connections) – cane Oct 19 '15 at 08:46

0 Answers0