1

i am trying to connect test a normal insertion code into mysql database using hibernate and getting error for this, i write the exception details below.

log4j:WARN No appenders could be found for logger (org.jboss.logging). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [/home/controller/Documents/sts_work/webtracker/src/main/java/hibernate.cfg.xml] at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53) at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163) at org.hibernate.cfg.Configuration.configure(Configuration.java:258) at testHBConnection.main(testHBConnection.java:12) 

and my java file for test is

import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import com.ar.entity.Student; public class testHBConnection { public static void main(String[] args) { // TODO Auto-generated method stub SessionFactory sf=new Configuration().configure("/home/controller/Documents/sts_work/webtracker/src/main/java/hibernate.cfg.xml") .addAnnotatedClass(Student.class) .buildSessionFactory(); Session ses=sf.getCurrentSession(); try { Student stu= new Student("","name"); ses.beginTransaction(); ses.save(stu); ses.getTransaction().commit(); } catch(Exception e) { System.out.println("Exception "+e); } finally{ } } } 

and i linked here my project structure below

where Student is the pojo file releted to database table how can i solve this problem?

1
  • Are you sure, hibernate.cfg.xml is present in the absolute location? Commented Nov 2, 2017 at 7:57

1 Answer 1

3

You can't use this path

/home/controller/Documents/sts_work/webtracker/src/main/java/hibernate.cfg.xml 

because of configure() method waits for class path resource (loaded by a classloader) or URL.

Better to use a resource, of course. So, if you have hibernate.cfg.xml in the src/main/java/ you don't need to specify the path to it at all:

new Configuration().configure() 

There is one tricky thing. It will work when you build a jar, for example and run it. if you will want to run application from the IDE, in some cases, better to check that hibernate.cfg.xml in the class path in the project configuration.

But the more professional way put hibernate.cfg.xml in the resourcesfolder, if you use Maven or gradle, for example.

Sign up to request clarification or add additional context in comments.

3 Comments

the problem is solved after relocated cfg.xml file to src/main/java
@AllahRakha It was already be there. No? src/main/java/hibernate.cfg.xml
no,i relocated it at src/main/java/ , and now it working

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.