I have been trying to go complete my first Hibernate tutorials using eclipse and connecting to an Oracle Database.
Using Hibernate tools I have created the console configuration, the hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.password">XXXXXXXX</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@abcdev.iers.dta.stiop.mi.ui:1561:erddev2</property>
<property name="hibernate.connection.username">USER001</property>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
</session-factory>
</hibernate-configuration>
The POJO file and the XML mapping document for the table I want to read was also successfully created using Hibernate tools. I also created a class to loop through the Business table and send the Business names to the console. The code is provided below:
package gov.mt.dli.erd.clm;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class FirstExample
{
public static void main(String[] args)
{
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
//session = InitSessionFactory.getInstance().getCurrentSession();
Transaction tx = session.beginTransaction();
List result = session.createQuery("from Business").list();
Iterator iter = result.iterator();
while (iter.hasNext())
{
Business aBusiness = (Business)iter.next();
System.out.println("Name: " +
aBusiness.getBusName());
}
tx.commit();
session.close();
}
}
When I try to run it I get the following error messages sent to the eclipse 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: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1329)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1351)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1338)
at gov.mt.dli.erd.clm.FirstExample.main(FirstExample.java:15)
The searches I have done on this error tell me my hibernate.cfg.xml file needs to be in the “root of my classpath”. I need confirmation on just how to do this in the eclipse environment. From the Package Explorer I have a package, Hibernate.Tutorial and have this file directly in this root. I have also tried to go through the Computer Environment variables for Classpath and but it there, but to no avail. I am new to Java and eclipse so I know I need to come up to speed in this area but, if someone would be so kind as to explain how to resolve this and if I don’t have the hibernate.cfg.xml in the right place give me a detailed explanation on how to get it there.
Thanks in advance.
|