0

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 project properly. In detail it builds a jar file which contains my CustomController.class and other custom *.class files and push that jar into the right directory on the server. If I also put the hibernate.cfg.xml file like my CustomController.class file I'm not able to load this file via

Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

3 Answers 3

1

I assume that your classpath is set to the src directory. Then you should use

Configuration cfg = new Configuration().configure("resources/hibernate.cfg.xml");
Sign up to request clarification or add additional context in comments.

3 Comments

@v.ladynev sorry yes you are right. configure would look in the classpath as well
I think, It is ok.
@simas_ch with class path you mean sources under project properties >java build path >source?
0

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;

3 Comments

I think, It is not correct. You can do it, if you pass a valid URL file:/c:/some_path.
Yes you're right i just copied his link without making attention
Your edit is incorrect too. Hibernate loads this file using URL or ClassLoader.
0

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;
    }

16 Comments

@StellaMaris There is a folder in your project which Eclipse uses to build files — bin or build.
This is my project structure. I downloaded this project here. And I added sample/customcontroller/src/resources to my java build path > sources.
I build a jar file which doesn't contains hibernate.cgf.xml file. I also tried the build after I put the file into several places in the project directory. When I was playing around with Spring-Hibernate. The only possibility to load the BeanLocation.xml was via "classpath*:BeanLocation.xml". But then it stops by loading the files which are written in the BeanLocation.xml
@StellaMaris There is a snippet in my answer from Hibernate sources. It is the way how Hibernate try to load hibernate.cfg.xml. Try to load this file yourself, for an example using a class loader.
You mean to load the file via File conf = new File("path/hibernate.cfg.xml"); or InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("path/hibernate.cfg.xml"); but I dont know the exact path like before.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.