如何将属性从hibernate.cfg.xml作为字符串加载到我的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();
}
}发布于 2016-05-19 18:01:30
您不能在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
https://stackoverflow.com/questions/37330319
复制相似问题