load hibernate.cfg.xml in existing java project

后端 未结 3 874
独厮守ぢ
独厮守ぢ 2021-01-26 05:55

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

相关标签:
3条回答
  • 2021-01-26 06:49

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

    Configuration cfg = new Configuration().configure("resources/hibernate.cfg.xml");
    
    0 讨论(0)
  • 2021-01-26 06:56

    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;
    
    0 讨论(0)
  • 2021-01-26 06:57

    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;
        }
    
    0 讨论(0)
提交回复
热议问题