I try to load hibernate.cfg.xml in a submodule (ssf/samples/customcontroller) of an existing project. For this project there exists a build.xml which builds and deploys the proj
I assume that your classpath is set to the src directory. Then you should use
Configuration cfg = new Configuration().configure("resources/hibernate.cfg.xml");
HI try to pass hibernate.cfg.xml
file path as an argument into the configure()
SessionFactory sessionFactory = new Configuration().configure(
"/ssf/samples/customcontroller/src/resources/hibernate.cfg.xml")
.buildSessionFactory();
return sessionFactory;
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;
}