See where hibernate.cfg.xml resides in the build folder (in which eclipse compiles classes).
If hibernate.cfg.xml in the root of the build folder
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
If hibernate.cfg.xml in the build_folder/some_path/some_other_path/hibernate.cfg.xml
SessionFactory sessionFactory = new Configuration()
.configure("/some_path/some_other_path/hibernate.cfg.xml")
.buildSessionFactory();
You can use path without the leading /, because of Hibernate deletes it anyway (it is even more correct way, but not clear, because of a path to a resource has the leading / usually)
SessionFactory sessionFactory = new Configuration()
.configure("some_path/some_other_path/hibernate.cfg.xml")
.buildSessionFactory();
This code shows how Hibernate tries to load hibernate.cfg.xml
public InputStream locateResourceStream(String name) {
// first we try name as a URL
try {
log.tracef( "trying via [new URL(\"%s\")]", name );
return new URL( name ).openStream();
}
catch (Exception ignore) {
}
try {
log.tracef( "trying via [ClassLoader.getResourceAsStream(\"%s\")]", name );
final InputStream stream = getAggregatedClassLoader().getResourceAsStream( name );
if ( stream != null ) {
return stream;
}
}
catch (Exception ignore) {
}
final String stripped = name.startsWith( "/" ) ? name.substring( 1 ) : null;
if ( stripped != null ) {
try {
log.tracef( "trying via [new URL(\"%s\")]", stripped );
return new URL( stripped ).openStream();
}
catch (Exception ignore) {
}
try {
log.tracef( "trying via [ClassLoader.getResourceAsStream(\"%s\")]", stripped );
final InputStream stream = getAggregatedClassLoader().getResourceAsStream( stripped );
if ( stream != null ) {
return stream;
}
}
catch (Exception ignore) {
}
}
return null;
}