hibernate.cfg.xml not found

前端 未结 7 2089
野的像风
野的像风 2020-12-06 05:46

I am new to Hibernate, reading this book \"Java persistence with Hibernate\" and I am trying to implement the example from there. So far my Ant build is successful, but when

相关标签:
7条回答
  • 2020-12-06 06:26

    The XML configuration file is by default expected to be in the root of your CLASSPATH.

    You can select a different XML or path configuration file using:

            SessionFactory sessionFactory;
            try {
                Logger log = Logger.getLogger(LOG);
                final String HIB_CONFIG = "/path/to/hibernate.cfg.xml";
    
                final File hibernate = new File(HIB_CONFIG);
                log.info("Try to init SessionFactory: " + HIB_CONFIG);
    
                // Create the SessionFactory from hibernate.cfg.xml
                if (hibernate.exists()) {
                    log.info("File exists. Init with custom file.");
                    sessionFactory = new Configuration()
                            .configure(hibernate)
                            .buildSessionFactory();
                } else {
                    log.info("File doesn't exist. Init with default project file.");
                    sessionFactory = new Configuration().configure().buildSessionFactory();
                }
            } catch (Throwable ex) {
                throw new ExceptionInInitializerError(ex);
            }
    

    For more information look to session-configuration

    0 讨论(0)
提交回复
热议问题