-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 
Author Message
 Post subject: Class cant find my hibernate.cfg.xml, why?
PostPosted: Wed Mar 09, 2005 4:08 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Hibernate version: 2.0

Hi there,
Im working with hibernate and when I try and run the class that does all the magic, I get a nasty exception:

Code:
net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
   at net.sf.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:694)
   at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:717)
   at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:705)
   at hbn.GetReport.main(GetReport.java:29)
Exception in thread "main"


This is impossible because I pasted the hibernate.cfg.xml file in every sub directory I could find. Can anyone help me?

Code:
package hbn;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import java.util.*;

public class GetReport {
   public GetReport(){
      
   }
   
public static void main(String[]args) throws Exception{

   SessionFactory sf = new Configuration().configure().buildSessionFactory();
   Session sess = sf.openSession();
   Transaction tx = null;
   try {
      tx = sess.beginTransaction();

      List ls = sess.find("from Customer");
      for (Iterator it = ls.iterator(); it.hasNext();) {
         Customer cr = (Customer) it.next();
         System.out.println( "Animal '" + cr.getName() +
         "' its class is: " + cr.getClass().getName());
         System.out.print("Makes sound: ");
         
         }
      tx.commit();
   } catch (HibernateException e) {
      if (tx!=null) tx.rollback();
      throw e;
      }
      finally {
      sess.close();
      }
}
}


Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
    <class
        name="Customer"
        table="customers"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="long"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Customer.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <set
            name="orders"
            lazy="false"
            inverse="false"
            cascade="none"
            sort="unsorted"
        >

              <key
                  column="customer_id"
              >
              </key>

              <one-to-many
                  class="Order"
              />

        </set>

        <!--
            To add non XDoclet property mappings, create a file named
                hibernate-properties-Customer.xml
            containing the additional properties and place it in your merge dir.
        -->

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject: forgot something
PostPosted: Wed Mar 09, 2005 4:28 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Just forgot to explain my project layout. Im using eclipse. In my project I have a hbn package and in there is all my classes and mapping files. The
hibernate.cfg.xml file was originaly in the project folder till I went ape and copied it into every sub-folder I could get. I read up on the same error on this forum and it seems that you have to put the file in a classes folder. This does not apply to this problem cause my project path is the root of this project.


Top
 Profile  
 
 Post subject: Re: forgot something
PostPosted: Wed Mar 09, 2005 4:41 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
srossouw wrote:
This does not apply to this problem cause my project path is the root of this project.


The root of the project may not be the same as the root of the compiled source code. All you need to do is ensure that once you have built/compiled, the hibernate.cfg.xml file ends up in the root of the compiled source code. So if you have a bin directory - it should end up in here.


Top
 Profile  
 
 Post subject: thanks for replying
PostPosted: Wed Mar 09, 2005 4:59 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
It is currently setup that way. My classes is in their package directories and the config file is in the same directory as the package directories:
Code:
ProjectFolder
             |
             +--->lib
             |
             +--->hbn     
             |       |
             |       +---> hbn.GetReport.class
             |       +---> Customer.hbm.xml
             |       +---> hbn.Customer.class
             |
             +--->hibernate.cfg.xml
             +--->build.xml


Top
 Profile  
 
 Post subject: Re: forgot something
PostPosted: Wed Mar 09, 2005 6:44 am 
Beginner
Beginner

Joined: Wed May 05, 2004 3:26 am
Posts: 25
As drj correctly pointed out the hibernate.cfg.xml should be in the same directory where the compiled classes go.

Assuming that hbn is the directory, hibernate.cfg.xml should go in there.

When developing under the Eclipse environment, you could put the hibernate.cfg.xml file in the "/src" directory.
This way you ensure that the file will be copied in the generated classes directory when compiling the project.

_________________
The Bits Control The Atoms


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 6:51 am 
Newbie

Joined: Wed Mar 09, 2005 4:25 am
Posts: 9
Location: Hamburg, Germany
Yes, the bibernate.cfg.xml should be placed in the out folder. But the build within eclipse should copy these file to your out folder.
Try to use:
Code:
URL configureURL = MyClass.class.getRessource("/hibernate.cfg.xml");
... new Configuration().configure(URL configureURL)...


Top
 Profile  
 
 Post subject: still not working
PostPosted: Wed Mar 09, 2005 10:27 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
I copied that stupid file in all the directories I could find, still not working, sort of frustrating. Maybe I should specify it somewhere in eclipse.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 10:31 am 
Newbie

Joined: Wed Mar 09, 2005 4:25 am
Posts: 9
Location: Hamburg, Germany
What is your output-dir in your java-project in eclipse ?


Top
 Profile  
 
 Post subject: default output path
PostPosted: Wed Mar 09, 2005 11:10 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
If I right click on the project in eclipse and I click on Java Build Path, the default output folder is "ProjectFolder". Tried to change it to "/ProjectFolder" but it reverts back to "ProjectFolder" when I close the window.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 09, 2005 11:15 am 
Newbie

Joined: Wed Mar 09, 2005 4:25 am
Posts: 9
Location: Hamburg, Germany
Perhaps you should define an output-folder (no mix-up of source & compiler-output), e.g. bin.
But placing the hibernate.cfg.xml in the project root should work.


Top
 Profile  
 
 Post subject: Re: default output path
PostPosted: Wed Mar 09, 2005 12:33 pm 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
srossouw wrote:
If I right click on the project in eclipse and I click on Java Build Path, the default output folder is "ProjectFolder". Tried to change it to "/ProjectFolder" but it reverts back to "ProjectFolder" when I close the window.


You need to get your project structure sorted out as afleischer has suggested. Clean separation of source and binary (class) files is an industry standard.


Top
 Profile  
 
 Post subject: worked!
PostPosted: Thu Mar 10, 2005 8:24 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Did this and it worked:
Code:
   File fl = new File("./bin/hibernate.cfg.xml");
   SessionFactory sf = new Configuration().configure(fl).buildSessionFactory();


But now I get this paranormal exception, any ideas?

Code:
(cfg.Environment                     378 ) Hibernate 2.0 final
(cfg.Environment                     407 ) hibernate.properties not found
(cfg.Environment                     427 ) using CGLIB reflection optimizer
(cfg.Environment                     437 ) JVM proxy support: true
(cfg.Configuration                   270 ) Mapping resource: Customer.hbm.xml
(mapping.Collection                  174 ) Mapping class: hbn.Customer -> customers
(cfg.Configuration                   871 ) Configured SessionFactory: null
java.lang.ExceptionInInitializerError
   at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:613)
   at hbn.GetReport.main(GetReport.java:31)
Caused by: java.lang.IllegalStateException: No backend found
   at net.sf.cglib.CodeGenerator.<init>(CodeGenerator.java:127)
   at net.sf.cglib.KeyFactoryGenerator.<init>(KeyFactoryGenerator.java:93)
   at net.sf.cglib.KeyFactory.create(KeyFactory.java:114)
   at net.sf.hibernate.impl.SessionFactoryImpl.<clinit>(SessionFactoryImpl.java:318)
   ... 2 more
Exception in thread "main"


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 10, 2005 10:17 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Last error means asm.jar is missing or you use wrong cglib jar (I am not sure about the right version for hibernate 2.0 ), just try to upgarde and remove all unused stuff from classpath. Runtime classpath and Build class path is not the same on Eclipse, it was a cause the first exception too.


Top
 Profile  
 
 Post subject: cglib version
PostPosted: Fri Mar 11, 2005 4:25 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Downloaded cglib-2.0.2.jar and asm-1.5.3.jar and now I get this error. Checked the package and found that KeyFactory is in net/sf/cglib/core, does that mean that the version of cglib is not compatible with hibernate2.0? What cglib should i get then, the previous version I used was 1.0

Code:
(cfg.Environment                     378 ) Hibernate 2.0 final
(cfg.Environment                     407 ) hibernate.properties not found
(cfg.Environment                     427 ) using CGLIB reflection optimizer
(cfg.Environment                     437 ) JVM proxy support: true
(cfg.Configuration                   270 ) Mapping resource: Customer.hbm.xml
(mapping.Collection                  174 ) Mapping class: hbn.Customer -> customers
(cfg.Configuration                   871 ) Configured SessionFactory: null
java.lang.NoClassDefFoundError: net/sf/cglib/KeyFactory
   at net.sf.hibernate.impl.SessionFactoryImpl.<clinit>(SessionFactoryImpl.java:318)
   at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:613)
   at hbn.GetReport.main(GetReport.java:31)
Exception in thread "main"


Top
 Profile  
 
 Post subject: Success at last!!!
PostPosted: Fri Mar 11, 2005 5:53 am 
Beginner
Beginner

Joined: Mon Mar 07, 2005 12:02 pm
Posts: 39
Code:
(cfg.Environment                     378 ) Hibernate 2.0 final
(cfg.Environment                     407 ) hibernate.properties not found
(cfg.Environment                     427 ) using CGLIB reflection optimizer
(cfg.Environment                     437 ) JVM proxy support: true
(cfg.Configuration                   270 ) Mapping resource: Customer.hbm.xml
(mapping.Collection                  174 ) Mapping class: hbn.Customer -> customers
(cfg.Configuration                   871 ) Configured SessionFactory: null
(impl.SessionFactoryImpl             140 ) building session factory
(dialect.Dialect                     37  ) Using dialect: net.sf.hibernate.dialect.MySQLDialect
(connection.DriverManagerConnectionProvider 41  ) Hibernate connection pool size: 20
(connection.DriverManagerConnectionProvider 70  ) using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/hibernatetest
(connection.DriverManagerConnectionProvider 71  ) connection properties: {user=root}
(impl.SessionFactoryImpl             170 ) Use outer join fetching: false
(impl.SessionFactoryImpl             193 ) Use scrollable result sets: true
(impl.SessionFactoryImpl             194 ) JDBC 2 max batch size: 15
(impl.SessionFactoryObjectFactory    82  ) no JDNI name configured
(impl.SessionFactoryImpl             287 ) Query language substitutions: {}
Customer No: 1, Name: Stephan
Customer No: 2, Name: Gawie
Customer No: 3, Name: Clint
Customer No: 4, Name: Manfred


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.