hibernate.cfg.xml not found

前端 未结 7 2104
野的像风
野的像风 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条回答
  •  Happy的楠姐
    2020-12-06 06:12

    You can load hibernate.cfg.xml from a different directory (not necessarily the classpath) using the configure(File configFile) method that takes the hibernateConfig File argument. (note, I am using hibernate 4.3.7)

    Like this:


    String hibernatePropsFilePath = "/etc/configs/hibernate.cfg.xml";
    
    File hibernatePropsFile = new File(hibernatePropsFilePath);
    
    Configuration configuration = new Configuration(); 
    configuration.configure(hibernatePropsFile);
    
    StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
    
    ServiceRegistry serviceRegistry = serviceRegistryBuilder.build();
    
    SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    

提交回复
热议问题