hibernate.cfg.xml没有找到hibernate.properties文件 [英] hibernate.cfg.xml does not find hibernate.properties file
本文介绍了hibernate.cfg.xml没有找到hibernate.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
解决方案
编程方式,您可以像这样加载XML和属性:
public class MyHibernate {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory(){
try {
URL r1 = MyHibernate.class.getResource(/ hibernate.cfg.xml);
配置c =新配置()。configure(r1);
try {
InputStream is = MyHibernate.class.getResourceAsStream(/ hibernate.properties);
属性props = new Properties();
props.load(is);
c.addProperties(道具);
} catch(Exception e){
LOG.error(Error reading properties,e);
}
return c.buildSessionFactory();
} catch(Throwable ex){
LOG.error(Error Creating SessionFactory,ex);
抛出新的ExceptionInInitializerError(ex);
public static SessionFactory getSessionFactory(){
return sessionFactory;
希望它有用。
We used to have the hibernate.cfg.xml and hibernate.properties files in the same folder in our Java source folder. Now I moved the hibernate.properties file to another location. How can I make the hibernate.cfg.xml file find the hibernate.properties file again? I get an error which seems to indicate that it is not found anymore.
解决方案
Programmatically, you can load XML and properties like this:
public class MyHibernate {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
URL r1 = MyHibernate.class.getResource("/hibernate.cfg.xml");
Configuration c = new Configuration().configure(r1);
try {
InputStream is = MyHibernate.class.getResourceAsStream("/hibernate.properties");
Properties props = new Properties();
props.load(is);
c.addProperties(props);
} catch (Exception e) {
LOG.error("Error reading properties", e);
}
return c.buildSessionFactory();
} catch (Throwable ex) {
LOG.error("Error creating SessionFactory", ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Hope it be useful.
这篇关于hibernate.cfg.xml没有找到hibernate.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文