我如何将hibernate.cfg.xml
的属性作为String加载到我的java程序中?
我想使用此属性创建IDataBaseTester
或告诉我IDataBaseTester
hibernate.cfg.xml
的另一种方法
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();
}
}
答案 0 :(得分:0)
您无法将AnnotationConfiguration
与Hibernate一起使用4.只需使用Configuration
。
从hibernate.cfg.xml
Configuration configuration = new Configuration().configure();
Properties properties = configuration.getProperties();
或
StandardServiceRegistryBuilder registryBuilder =
new StandardServiceRegistryBuilder().configure();
Map map = registryBuilder.getSettings();
或者您可以尝试使用 ConfigLoader