user636207
user636207

Reputation: 286

Cannot find hibernate.cfg.xml

I am new to hibernate.. Iam using netbeans IDE and i setup every thing.. I placed all my stuff(configuration and mapping files) into a package called myfirsthibernate while running the sample application it is showing the error as couldn't find the file hibernate.cfg.xml. But it is there in myfirsthibernate package. How to rectify this and why this is happening??

Please help me and thanks..

Upvotes: 1

Views: 6927

Answers (3)

Paul Mira
Paul Mira

Reputation: 71

Assuming your project structure looks something like this src>myfirsthibernate>[some files], the Hibernate Documentation says hibernate expects to find the hibernate.cfg.xml file at the root of the classpath by default. So in order to start hibernate like this

SessionFactory sf = new Configuration().configure().buildSessionFactory();

your directory structure should look like this

>src
   >myfirsthibernate
      >[your entity classes and mapping files here]
   >hibernate.cfg.xml

If you wish to keep the file under myfirsthibernate package, then you need to add it's location during the configuration :

SessionFactory sf = new Configuration() .configure("myfirsthibernate/hibernate.cfg.xml") .buildSessionFactory();

Upvotes: 0

Vikram Subramanian
Vikram Subramanian

Reputation: 61

By default, the hibernate.cfg.xml file is placed in /src/java/resource (which is in classpath by default directory). Could you please try placing the cfg file there?

Could you also give me some more info about the directory structure of your project.

Upvotes: 3

mea
mea

Reputation: 71

The hibernate.cfg.xml needs to be in your classpath as well. Maybe write an ant task that automates this copying for you. Also, see hibernate.cfg.xml not found .

Upvotes: 1

Related Questions