我需要为hibernate创建一个会话工厂,但是默认情况下它会寻找“ hibernate.cfg.xml”文件。我没有创建该文件。所有的休眠配置都在Spring MVC配置的“ applicationContext.xml”文件中。我需要做的就是为Configuration类的“ configure”方法提供“ / WEB-INF / applicationContext”文件路径,但是我不知道如何在JAVA中找到该文件的相对路径。
public static SessionFactory createSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure(HERE I NEED TO GET "/WEB-INF/applicationContext.xml FILE PATH");
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory; }
休眠加载/WEB-INF/applicationContext.xml
使用ClassLoader.getResourceAsStream("/WEB-INF/applicationContext.xml")
ClassLoader
尝试访问类路径根目录中的文件-对于Web应用程序,它为war/WEB-INF/classes/
。你应该把applicationContext.xml
到war/WEB-INF/classes/applicationContext.xml
和使用加载它
sessionFactory = new Configuration().configure("applicationContext.xml")
.buildSessionFactory();
您不能指定/WEB-INF/applicationContext.xml
,因为/WEB-INF/
不在类路径中。
如果你真的想要得到的配置从/WEB-INF/applicationContext.xml
你应该得到URL
的applicationContext.xml
使用ServletContext
:文件路径的资源在我们的战争/ WEB-INF文件夹?
并将其传递URL
给Configuration
or StandardServiceRegistryBuilder
,这取决于Hibernate版本。
重要提示
我认为,上面是没有意义的,因为applicationContext.xml
应该具有类似的结构hibernate.cfg.xml
,而显然没有。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句