![](/img/trans.png)
[英]hibernate.cfg.xml does not find hibernate.properties file
[英]properties hibernate.cfg.xml
我如何将hibernate.cfg.xml
属性作为String加载到我的Java程序中?
我想使用此属性创建IDataBaseTester
,或者告诉我从hibernate.cfg.xml
创建IDataBaseTester
另一种方法
HibernateUtil
用于创建会话
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
您不能在Hibernate 4中使用AnnotationConfiguration
。只能使用Configuration
。
从hibernate.cfg.xml
获取属性
Configuration configuration = new Configuration().configure();
Properties properties = configuration.getProperties();
要么
StandardServiceRegistryBuilder registryBuilder =
new StandardServiceRegistryBuilder().configure();
Map map = registryBuilder.getSettings();
或者您可以尝试使用ConfigLoader
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.