This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate.cfg.xml not found

 
Natalia Lopez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!!
I'm new with hibernate. I'm trying to make an example with mysql and hibernate using JBuiderX
I defined a class Singleton with the following code

public class Singleton {

private SessionFactory sessionFactory;

public Singleton() {
try {
System.out.println("Initializing Hibernate");
sessionFactory = new Configuration().configure().buildSessionFactory();
System.out.println("Finished Initializing Hibernate");
} catch (HibernateException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
Singleton instance = new Singleton();
System.exit(0);
}

}

as the hibernate documentation say. But when I run the Singleton to see if I can connect, I obtain the following error

log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

net.sf.hibernate.HibernateException: /hibernate.cfg.xml not found
at net.sf.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:872)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:896)
at net.sf.hibernate.cfg.Configuration.configure(Configuration.java:883)
at com.tutorial.hibernate.db.Singleton.<init>(Singleton.java:23)
at com.tutorial.hibernate.db.Singleton.main(Singleton.java:31)

The hibernate.cfg.xml is in the src directory
Anybody can help me!!!
REgards

Naty
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Natalia"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hibernate looks for that file to be in the root of one of the JARs or directories on the classpath. You should be able to add a directory to the classpath in JBuilder and put the file there.
 
Natalia Lopez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!!
I put the hibernate.cfg.xml on the class directory and work but still not found the maping.hbm.xml files.
The src directory is in the class path and I put hibernate.cfg.xml and the mapping.hbm.xml in the src directory. I wonder why they are not founded?
There is some option n JBuilder that reject .xml files in the src directory?
Regards

Naty
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you moved the hibernate.cfg.xml file from your classes directory to the source directory, did it still find it? If so, then clearly Hibernate will access XML files in the source tree. If this is the case, make sure your mapping files are where Hibernate expects them.

If you're telling Hibernate about a mapped class (rather than the file itself), Hibernate will look in the same package as the file. If you specify the file directly, double-check your paths. Also, maybe post up the code you're using to perform the configuration.

If you don't get it, I'll ask one of the developers on my team that uses Eclipse how they have it set up for running JUnit tests from Eclipse which require the mapping files.
 
Natalia Lopez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again
When you moved the hibernate.cfg.xml file from your classes directory to the source directory, did it still find it?
The answer is not
I put the mapped class in the same directory than the .java class definition as the documentation say.
I will go to apreciate some example in eclipse, maybe can help me !!!
Or maybe JBuilder need some other configuration I really don't know.
Just in case I'm using JBuilderX
King Regards

Naty
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, none of us uses JBuilder anymore, but I'll ask if anyone remembers how to add the source directory to the class path. In the project properties, there should be a way to add any directory to the classpath, just like adding a JAR but maybe a different tab page. Is there nothing like that?
 
Natalia Lopez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I put in lib directory where I have the .jar 's files but still not work. Only find it If I put in classes directory but when I rebuilt the project I lost the file
I will write you If I get a solution
Thank you

Naty
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look under Project Properties>Build>Resource there is a list of file types which will get copied as part of your build process. You should be able to add *.cfg.xml and *.hbm.xml (or possibly just *.xml) in there.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find this setting in Eclipse 3.0.1 or in Eclipse 2.1. Am I missing something?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are. Natalia Lopez is using JBuilderX....
[ February 23, 2005: Message edited by: Paul Sturrock ]
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, my bad. I sent Steven to this thread but didn't make the connection. Sorry Steven!
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
log4j:WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

this means you dont have a log4j.properties file in your classpath

imagine your root folder is:
MyProject
inside, you'll have two folders:
MyClasses and MySources
Inside each one you'll have a package named MyPackage where respectively you'll keep *.class and *.java files
Put hibernate.cfg.xml and log4j.properties inside:
MyProject/MyClasses

and later on add the *.hbm.xml to
MyProject/MyClasses/MyPackage,
together with your class files
hope this helps
good luck
 
Natalia Lopez
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody!!!
Thanks a lot for help!
I finally resolve the problem enabling .xml extension in the configuration of JBuilder. Now in the src I can see the .java and the .xml files and when rebuilt all is on classes folder.
That simplicity was all the problem and solution!
Till the next

Naty
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic